Skip to content

Instantly share code, notes, and snippets.

@arto-heino
Created June 7, 2017 12:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arto-heino/290c4cf553e7fbc503eac6a822a21b8e to your computer and use it in GitHub Desktop.
Save arto-heino/290c4cf553e7fbc503eac6a822a21b8e to your computer and use it in GitHub Desktop.
I have two arrays, questions and answers. I need to merge them together so in question there are its answer if it have answer.
This is how it should look like:
array(‘questions’ =>
array(‘id’=> 1,
’question’ => ‘How sick you are’,
‘answer’ => 3))
And if there are no answer it should only show the question.
Questions array:
array(‘Questions =>
array(‘id’ => 1,
‘question’ => ‘How sick you are’))
Answers array:
array(‘Answers =>
array(‘id’ => 5,
‘question_id => ‘1’,
‘answer’ => ‘3’))
I have been trying to foreach the Questions and Answers like this,
foreach($questions as $key_kys => $value_kys){
foreach($answers as $key_vas => $value_vas){
if($value_kys['TherapygroupQuestions']['id'] == $value_vas['TherapygroupAnswers']['question_id']){
$questions['vastaus'] = $value_vas['TherapygroupAnswers']['vastaus'];
$questions['vastaus_id'] = $value_vas['TherapygroupAnswers']['id'];
}
}
And with array_merge or array_push.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment