<?php
$listOfEmployees = array();
$Employee = new StdClass();
$Employee->department = 'foo';
$Employee->name = 'person1';
$listOfEmployees[] = $Employee;
$Employee = new StdClass();
$Employee->department = 'foo';
$Employee->name = 'person2';
$listOfEmployees[] = $Employee;
$Employee = new StdClass();
$Employee->department = 'bar';
$Employee->name = 'person3';
$listOfEmployees[] = $Employee;
$listOfGroupedEmployess = array();
foreach($listOfEmployees as $Employee)
{
$listOfGroupedEmployess[$Employee->department][] = $Employee;
}
var_dump($listOfGroupedEmployess);
- Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.26, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.29 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- array(2) {
["foo"]=>
array(2) {
[0]=>
object(stdClass)#1 (2) {
["department"]=>
string(3) "foo"
["name"]=>
string(7) "person1"
}
[1]=>
object(stdClass)#2 (2) {
["department"]=>
string(3) "foo"
["name"]=>
string(7) "person2"
}
}
["bar"]=>
array(1) {
[0]=>
object(stdClass)#3 (2) {
["department"]=>
string(3) "bar"
["name"]=>
string(7) "person3"
}
}
}
- Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9
- array(2) {
["foo"]=>
array(2) {
[0]=>
object(stdClass)(2) {
["department"]=>
string(3) "foo"
["name"]=>
string(7) "person1"
}
[1]=>
object(stdClass)(2) {
["department"]=>
string(3) "foo"
["name"]=>
string(7) "person2"
}
}
["bar"]=>
array(1) {
[0]=>
object(stdClass)(2) {
["department"]=>
string(3) "bar"
["name"]=>
string(7) "person3"
}
}
}
preferences:
201.42 ms | 402 KiB | 353 Q