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);

preferences:
24.98 ms | 406 KiB | 5 Q