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 } foreach ($unique as $key => $value) { $array[$value['id']] = $value; } echo '<pre>'; print_r($array);
based on 6P2gi
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.3.0alpha1
<pre>Array ( [message1] => Array ( [type] => voice [call-id] => 11 [id] => message1 ) [message3] => Array ( [type] => text [call-id] => 44 [id] => message3 ) [message5] => Array ( [type] => voice [call-id] => 55 [id] => message5 ) )