0

I am trying to rebuild php array, which I can get with one query from database. To build lists and add items I need the array to be grouped and rebuild. From query I receive array like this:

    array (
    0 => 
    array (
    'item_id' => '1',
    'item_name' => 'aaa',
    'group_id' => '7',
    'group_name' => 'first'
    ),
    1 => 
    array (
    'item_id' => '2',
    'item_name' => 'bbb',
    'group_id' => '7',
    'group_name' => 'first'
    ),
    2 => 
    array (
    'item_id' => '3',
    'item_name' => 'ccc',
    'group_id' => '9',
    'group_name' => 'second'
    ),
    3 => 
    array (
    'item_id' => '4',
    'item_name' => 'ddd',
    'group_id' => '9',
    'group_name' => 'second'
    )
    );

I need to rearrange this array to following, which I can use for js parsing:

    array(
    array ('group_id' => '7', 'group_name' => 'first', 'items' => array (
    0 => array (
    'item_id' => '1',
    'item_name' => 'aaa',
    ),
    1 => array (
    'item_id' => '2',
    'item_name' => 'bbb',
    )
    )
    ),
    array ('id' => '8', 'name' => 'second', 'items' => array (
    0 => 
    array (
    'item_id' => '3',
    'item_name' => 'ccc',
    ),
    1 => 
    array (
    'item_id' => '4',
    'item_name' => 'ddd',
    )
    )
    )
    );

I have stuck with following code:

    function rebuild($groupby, $param, $data) {
    $newarray = array();
    foreach($data as $key => $val) {
        if(array_key_exists($groupby, $val)){
            $newarray[$val[$groupby]] = $val[$param];
            $newarray['items'][] = $val;    
        }else{
        $newarray[""][] = $val;
       }
    }
    return $newarray;
    }
    $show = rebuild('group_id', 'group_name', $data);
    echo "<pre>" . var_export($show, true) . "</pre>";

Any advise would appreciated.

1 Answer 1

2

I always like to use the unique identifier (here, the group ID), as array key in the resulting array - that makes it easier to associate the right items to the right new entry, without having to “search” the new array for the entry with the matching identifier.

$newarray = array();
foreach($data as $item) {
  $newarray[$item['group_id']]['group_id'] = $item['group_id'];
  $newarray[$item['group_id']]['group_name'] = $item['group_name'];
  $newarray[$item['group_id']]['items'][] = [
    'item_id' => $item['item_id'],
    'item_name' => $item['item_name']
  ];
}
$newarray = array_values($newarray); // resets the array keys to a zero-based index
var_dump($newarray);

group_id and group_name get overwritten in the result array elements all the time - but since we access the correct entry by the group id already, that does not matter. They will always be “overwritten” with the values they already have, so that does no harm.

And item_id and item_name simply get added to the items array of the entry identified by the group id.

Sign up to request clarification or add additional context in comments.

1 Comment

Works nicely. Thanks a lot!

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.