3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = [ 0 => [ 'id' => 20, 'name' => 'Test' ], 1 => [ 'id' => 20, 'name' => 'Test1' ], 2 => [ 'id' => 12, 'name' => 'Test2' ], 3 => [ 'id' => 24, 'name' => 'Test3' ] ]; $arr = $currentValue = $previousValue = ['id' => '']; foreach($array as $key => $value) { $currentValue = [ 'id' => $previousValue['id'] === $value['id'] ? '' : $value['id'], 'name' => $value['name'] ]; $arr[] = $previousValue = $currentValue; } var_dump($arr);
based on FP1D0
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0rc3
array(5) { ["id"]=> string(0) "" [0]=> array(2) { ["id"]=> int(20) ["name"]=> string(4) "Test" } [1]=> array(2) { ["id"]=> string(0) "" ["name"]=> string(5) "Test1" } [2]=> array(2) { ["id"]=> int(12) ["name"]=> string(5) "Test2" } [3]=> array(2) { ["id"]=> int(24) ["name"]=> string(5) "Test3" } }