3v4l.org

run code in 300+ PHP versions simultaneously
<?php $items = array( array ( 'title'=>"a", 'filename'=>"1.jpg" ), array ( 'title'=>"a", 'filename'=>"2.jpg" ), array ( 'title'=>"b", 'filename'=>"1.jpg" ), array ( 'title'=>"b", 'filename'=>"2.jpg" ) ); // I have used below to create an stdClass object array, don't use this line in your code $items = json_decode(json_encode($items)); $finalArray = []; foreach($items as $item){ $finalArray[$item->title]['filename'][] = $item->filename; } foreach($finalArray as $key=>$value){ echo $key.PHP_EOL; foreach($value['filename'] as $val){ echo $val.PHP_EOL; } echo PHP_EOL; echo PHP_EOL; }
Output for 7.1.25 - 7.1.30, 7.2.0 - 7.2.33, 7.3.0 - 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
a 1.jpg 2.jpg b 1.jpg 2.jpg

preferences:
179.35 ms | 403 KiB | 168 Q