3v4l.org

run code in 300+ PHP versions simultaneously
<?php $answers = [ [ 'questionid' => '28f68dc4-acc6-4ab7-956d-ca41abf3a4c2', 'answer' => 'Antwoord 89', 'extra' => '', ], [ 'questionid' => '28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20', 'answer' => 'Antwoord 20', 'extra' => 'Extra informatie 20', ], [ 'questionid' => 'd8f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20', 'answer' => 'Antwoord 3', 'extra' => '', ], ]; $questions = [ [ 'questionId' => '28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20', 'question' => '1. Question 1', 'type' => 'typequestion/question-text-input', ], ]; $indexedQuestions = []; foreach ($questions as $question) { $id = $question['questionId']; $indexedQuestions[$id] = $question; } $combined = []; foreach ($answers as $answer) { $id = $answer['questionid']; if (isset($indexedQuestions[$id])) { $combined[] = $answer + $indexedQuestions[$id]; } else { $combined[] = $answer; } } print_r($combined);
Output for 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.26, 8.4.1 - 8.4.13
Array ( [0] => Array ( [questionid] => 28f68dc4-acc6-4ab7-956d-ca41abf3a4c2 [answer] => Antwoord 89 [extra] => ) [1] => Array ( [questionid] => 28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20 [answer] => Antwoord 20 [extra] => Extra informatie 20 [questionId] => 28f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20 [question] => 1. Question 1 [type] => typequestion/question-text-input ) [2] => Array ( [questionid] => d8f-28f68dc4-acc6-4ab7-956d-ca41abf3a4c20 [answer] => Antwoord 3 [extra] => ) )

preferences:
82.42 ms | 409 KiB | 5 Q