Created
June 14, 2017 08:16
-
-
Save arto-heino/819e9c6863371c69552c841a92609cfb to your computer and use it in GitHub Desktop.
Problem with array mergin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$questions = array('TherapygroupQuestions' => | |
array('id' => 1,'question' => "How much this hurt?"), | |
array('id' => 2, 'question' => "How much your feet hurt?"), | |
array('id' => 3, 'question' => "How much your hand hurt?") | |
); | |
$answers = array('TherapygroupAnswers' => | |
array('id' => 3, 'question_id' => 1, 'answer' => 1), | |
array('id' => 5, 'question_id' => 3, 'answer' => 5) | |
); | |
$result = Array(); | |
foreach ($questions as $key_1 => &$value_1) { | |
foreach ($answers as $key_1 => $value_2) { | |
if ($value_1['id'] == $value_2['question_id']) { | |
$result[] = array_merge($value_1, $value_2); | |
}else{ | |
array_push($result, $value_1); | |
} | |
} | |
} | |
It will result as: | |
Array ( [0] => Array ( [id] => 3 [question] => How much this hurt? [question_id] => 1 [answer] => 1 ) | |
[1] => Array ( [id] => 1 [question] => How much this hurt? ) | |
[2] => Array ( [id] => 2 [question] => How much your feet hurt? ) | |
[3] => Array ( [id] => 2 [question] => How much your feet hurt? ) | |
[4] => Array ( [id] => 3 [question] => How much your hand hurt? ) | |
[5] => Array ( [id] => 5 [question] => How much your hand hurt? [question_id] => 3 [answer] => 5 ) ) | |
It should result as: | |
Array ( [0] => Array ( [id] => 3 [question] => How much this hurt? [question_id] => 1 [answer] => 1 ) | |
[1] => Array ( [id] => 2 [question] => How much your feet hurt? ) | |
[2] => Array ( [id] => 5 [question] => How much your hand hurt? [question_id] => 3 [answer] => 5 )) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment