Splitting an array into multiple arrays according to a value

$oldArray = data; $newArray = array(); foreach($oldArray as $e){ if( !isset($newArray[$e->job]) ){ $newArray[$e->job] = array(); } $newArray[$e->job][] = $e; } var_dump($newArray);
Splitting an array containing objects and grouping it into many arrays according to a key. Like if we have an array containing 5 people; 3 are doctors and 2 are engineers. Splitting it to two arrays each contain people belonging to that profession then grouping it back again into one array containing two arrays!

1 Response

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.