Example: –
$arrayName = array(
'0' => array('name' => 'a', 'id' => '123' ),
'1' => array('name' => 'b', 'id' => '123' ),
'3' => array('name' => 'c', 'id' => '456' )
);
Same Id value should be grouped here
Result:
$arrayName = array(
'0' => array(
'0' => array('name' => 'a', 'id' => '123' ),
'1' => array('name' => 'b', 'id' => '123' ) ),
'1' => array('1' => array('name' => 'c', 'id' => '456' ) );
Using your example set, you can simply iterate through the values, and group the data based on
id. Using something likeThe output of that would be:
array(2) { [123]=> array(2) { [0]=> array(2) { ["name"]=> string(1) "a" ["id"]=> string(3) "123" } [1]=> array(2) { ["name"]=> string(1) "b" ["id"]=> string(3) "123" } } [456]=> array(1) { [0]=> array(2) { ["name"]=> string(1) "c" ["id"]=> string(3) "456" } } }If you really don’t want the
idto be the key, just grab the values from that associative array.array(2) { [0]=> array(2) { [0]=> array(2) { ["name"]=> string(1) "a" ["id"]=> string(3) "123" } [1]=> array(2) { ["name"]=> string(1) "b" ["id"]=> string(3) "123" } } [1]=> array(1) { [0]=> array(2) { ["name"]=> string(1) "c" ["id"]=> string(3) "456" } } }Note: There’s likely a better way to do this, using some of the other array functions; however, this shows the basic logic.