Reputation: 317
how do you chunk array like this below?
array(
[0] => array(
['id'] => '1',
['parent_id'] => ''
)
[1] => array(
['id'] => '2',
['parent_id'] => ''
)
[2] => array(
['id'] => '3',
['parent_id'] => '1'
)
[3] => array(
['id'] => '3',
['parent_id'] => '1'
)
[4] => array(
['id'] => '3',
['parent_id'] => '2'
)
)
I'd like to group subelements (with specified parent_id) to do a separate foreach.
Upvotes: 1
Views: 1984
Reputation: 2541
You can do a simple foreach and rearrange the array.
$result = array();
foreach ($arr as $element) {
$result[$element['parent_id']][] = $element;
}
http://phpfiddle.org/main/code/3ar-ixg
Upvotes: 4