3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = array( array( 'name' => 'A1', 'status' => 'Not Available', 'statusid' => 4, 'num' => 33, ), array( 'name' => 'A2', 'status' => 'Available', 'statusid' => 1, 'num' => 39, ), array( 'name' => 'A3', 'status' => 'Busy', 'statusid' => 3, 'num' => 55, ), array( 'name' => 'A4', 'status' => 'Available', 'statusid' => 1, 'num' => 54, ), array( 'name' => 'A5', 'status' => 'Busy', 'statusid' => 3, 'num' => 33, ), ); $new_array = array(); foreach($array as $a) { if(!isset($new_array[$a['num']]) || $a['statusid'] < $new_array[$a['num']]['statusid']) { $new_array[$a['num']] = $a; } } print_r($new_array); for($i=0;$i<6;$i++){ var_dump($new_array[$i]); }
based on HbP5o
Output for 5.6.0 - 7.3.0beta1
Array ( [33] => Array ( [name] => A5 [status] => Busy [statusid] => 3 [num] => 33 ) [39] => Array ( [name] => A2 [status] => Available [statusid] => 1 [num] => 39 ) [55] => Array ( [name] => A3 [status] => Busy [statusid] => 3 [num] => 55 ) [54] => Array ( [name] => A4 [status] => Available [statusid] => 1 [num] => 54 ) ) Notice: Undefined offset: 0 in /in/RbKjY on line 44 NULL Notice: Undefined offset: 1 in /in/RbKjY on line 44 NULL Notice: Undefined offset: 2 in /in/RbKjY on line 44 NULL Notice: Undefined offset: 3 in /in/RbKjY on line 44 NULL Notice: Undefined offset: 4 in /in/RbKjY on line 44 NULL Notice: Undefined offset: 5 in /in/RbKjY on line 44 NULL