0

I have following array :

[1] => Array
        (
            [entity_id] => 5877
            [parent_id] => 5862
            [label] => Railbikes
            [name] => railbikes
            [icon] => books.svg
            [level] => 5
            [tab_id] => 353
        )

    [2] => Array
        (
            [entity_id] => 5756
            [parent_id] => 5754
            [label] => Tournaments
            [name] => tournaments
            [icon] => books.svg
            [level] => 5
            [tab_id] => 354
        )

    [3] => Array
        (
            [entity_id] => 5756
            [parent_id] => 5754
            [label] => Tournaments
            [name] => tournaments
            [icon] => books.svg
            [level] => 5
            [tab_id] => 357
        )

In this array label => Tournaments repeats twice and this is the case for whole array many labels are repeating twice , thrice and many time . I want this array to be shown like that there will be a unique label and there is tab_id in each array which is different . This tab_id sholdd be appended to the unique label . The final array should look like this .

[1] => Array
            (
                [entity_id] => 5877
                [parent_id] => 5862
                [label] => Railbikes
                [name] => railbikes
                [icon] => books.svg
                [level] => 5
                [tab_id] => 353
            )

        [2] => Array
            (
                [entity_id] => 5756
                [parent_id] => 5754
                [label] => Tournaments
                [name] => tournaments
                [icon] => books.svg
                [level] => 5
                [tab_id] => 354 , 357
            )

Thanks.

4
  • 2
    Where are you getting this data from? If it's from an SQL query consider adding DISTINCT on entity_id and a GROUP_CONCAT on tab_id. Commented May 20, 2014 at 11:50
  • 1
    Can we see your PHP Code ? Commented May 20, 2014 at 11:50
  • 1
    [tab_id] => 354 , 357 is not valid syntax, it should be either a sub array, either a string Commented May 20, 2014 at 11:53
  • 1
    I don't see a question. I see the data that you have and the format you want it in. Do you want the code required to change one to the other? If yes, how are you getting the data and what have you tried so far? If no, what's the problem? Commented May 20, 2014 at 11:57

3 Answers 3

1
$arr = array(1 => array
    (
        entity_id => 5877,
        parent_id => 5862,
        label => Railbikes,
        name => railbikes,
        icon => books.svg,
        level => 5,
        tab_id => 353
    ),

2 => array
    (
        entity_id => 5756,
        parent_id => 5754,
        label => Tournaments,
        name => tournaments,
        icon => books.svg,
        level => 5,
        tab_id => 354
    ),

3 => array
    (
        entity_id => 5756,
        parent_id => 5754,
        label => Tournaments,
        name => tournaments,
        icon => books.svg,
        level => 5,
        tab_id => 357
    )
    );


    print("<pre>");
    foreach ($arr AS $key => $value){
        /*foreach ($value AS $innerKey => $innerValue){

        }*/

        if($arr[$key]['label']  == $arr[$key-1]['label'] )
        {
            $newArr[$key-1]['tab_id'] = $arr[$key]['tab_id'].",". $arr[$key-1]['tab_id'];
        }
        else{
            $newArr[$key]= $arr[$key];
        }
    }
            print_r($newArr);
Sign up to request clarification or add additional context in comments.

3 Comments

For the first $key there is no $key-1 value, you would add a condition for that. Also, this only works providing that the 2 objects with the same label are listed one after the other. And knowing that he has an entity_id, you should probably use that for identification and not the label.
I had used label as he has mentioned label as a unique value... for universal solution i need to test with the different cases...
2 more hick-ups: you should initiate $newArr, and if there is a duplicate you skip a $key when adding to $newArr (this can have issues if he has 3 items with the same label, as the third won't be added)
0

Try this out

$new_array = array();
$items = array();

foreach( $array as $item )
{
    $check = array_search( $item['entity_id'], $items, true ); 
    if ( $check === false )
    {
        $temp = $item;
        $temp['tab_id'] = array( $item['tab_id'] );
        $new_array[] = $temp;
        $items[] = $temp['entity_id'];
    }
    else
    {
        $new_array[$check]['tab_id'][] = $item['tab_id'];
    }
}

where $array is your original array the you showed us. You can replace it at the end with $array = $new_array; if you want to keep the same name in the rest of the code.

Also, keep in mind that tab_id is now an array to fit with your requirement.

If you really want to keep it as in your example, you could use

$new_array = array();
$items = array();

foreach( $array as $item )
{
    $check = array_search( $item['entity_id'], $items, true ); 
    if ( $check === false )
    {
        $new_array[] = $item;
        $items[] = $item['entity_id'];
    }
    else
    {
        $new_array[$check]['tab_id'] .= ', '.$item['tab_id'];
    }
}

Comments

0

If the entity_id is always the same then you can 'cheat' a bit:

$new_array = array();
foreach($array as $item)
{
    if(!isset($new_array[$item['entity_id']]) $new_array[$item['entity_id']] = $item;
}

$new_array = array_values($new_array);

PS: Always try to prevent double data, doesn't make your script faster ;)

3 Comments

Is this line if(!isset($new_array[$item['entity_id']]) $new_array[$item['entity_id']] = $item; written correctly ?
And I can use this by adding distinct to query , but my main goal is to combine tab_is as I have mentioned above .
Seems like it's missing a closing ) just before $new_array[$item['entity_id']] = $item;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.