3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $messages = array( 'message1'=>array( 'type'=>'voice', 'call-id'=>'11', 'id'=>'message1' ), 'message2'=>array( 'type'=>'voice', 'call-id'=>'44', 'id'=>'message2' ), 'message3'=>array( 'type'=>'text', 'call-id'=>'44', 'id'=>'message3' ), 'message4'=>array( 'type'=>'text', 'call-id'=>'55', 'id'=>'message4' ), 'message5'=>array( 'type'=>'voice', 'call-id'=>'55', 'id'=>'message5' ), ); foreach ($messages as $message => $value) { $unique[$value['call-id']] = $value; // this will override the previous value with the same key if exists } $unique = usort($unique, "cmp"); print_r($unique); foreach ($unique as $key => $value) { $array[$value['id']] = $value; } echo '<pre>'; print_r($array); function cmp($a, $b) { return $a["call-id"] - $b["call-id"]; }
based on 2Md6s
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0alpha1
1 Warning: Invalid argument supplied for foreach() in /in/blfIJ on line 40 <pre> Notice: Undefined variable: array in /in/blfIJ on line 45
Output for hhvm-3.15.4
1 Warning: Invalid argument supplied for foreach() in /in/blfIJ on line 42 <pre> Notice: Undefined variable: array in /in/blfIJ on line 45