3v4l.org

run code in 300+ PHP versions simultaneously
<?php $first = array ( 0 => array ( 'id' => 5, 'name' => 'Education' ), 1 => array ( 'id' => 4, 'name' => 'Computers' ), 3 => array ( 'id' => 7, 'name' => 'Science' ), 4 => array ( 'id' => 1, 'name' => 'Sports' ), ); $second = array ( 0 => array ( 'id' => 1, 'title' => 'Sport' ), 1 => array ( 'id' => 7, 'title' => 'Sci' ), 3 => array ( 'id' => 4, 'title' => 'Comp' ), 4 => array ( 'id' => 5, 'title' => 'Edu' ), ); $second = array_column($second, null, 'id'); foreach ($first as &$subject) { $subject = array_merge($subject, $second[$subject['id']] ?? []); } print_r($first);
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
Array ( [0] => Array ( [id] => 5 [name] => Education [title] => Edu ) [1] => Array ( [id] => 4 [name] => Computers [title] => Comp ) [3] => Array ( [id] => 7 [name] => Science [title] => Sci ) [4] => Array ( [id] => 1 [name] => Sports [title] => Sport ) )

preferences:
141.86 ms | 408 KiB | 5 Q