0

I have 2 table, that is a question_table and answer_table the structure is like this : enter image description here

And I have a JSON array that I got from question_table and answer_table using php like this.

// to get the question
$pertanyaan = "select * from question_table”;
$resultPertanyaan = mysqli_query($con, $pertanyaan);

while($rowQuery= mysqli_fetch_array($resultPertanyaan)){
   $array_question[]= 
array('id'=>$rowQuery['id’],’question’=>$rowQuery['question']);
}

and the result is like this

array_question :
[
    {
        "id": "8",
        “question”: "Shop sign/billboard  "
    },
    {
        "id": "10",
        "question": "Pylon"
    },
    {
        "id": “11”,
        “question”: "Banner”
    },
    {
        "id": "12”,
        "question": “Sport”
    },
   {
        "id": “14”,
        “question”: “Matic "
    },
    {
        "id": "16”,
        "question": “Cub”
    }
]

To get the answer

$jawaban = "select * from answer_table”;
$resultJawaban = mysqli_query($con, $jawaban);

while($rowQuery= mysqli_fetch_array($resultJawaban)){
    $array_answer[]=
array('id'=>$rowQuery['id'],'remark'=>$rowQuery['remark'],'item'=>$rowQuery['item']);
}

and the result like this

array_answer :
[
    {
        "id": "1b9fa84e-0f2f-11e9-b673-005056be36b2",
        “answer”: "3",
        “id_question”: "16"
    },
    {
        "id": "bc82c3fd-0f2e-11e9-b673-005056be36b2",
        "answer": "1",
        "id_question": "11"
    },
    {
        "id": "cc9363f1-0f2e-11e9-b673-005056be36b2",
        "answer": "3",
        "id_question": "12"
    },
    {
        "id": "f1dfa8b5-0f2e-11e9-b673-005056be36b2",
        "answer": "1",
        "id_question": "14"
    }
]

I want to combine array_answer with array_question, which results like this:

array_result :
[
    {
        "id": "8",
        “question”: "Shop sign/billboard  ",
        “asnwer” : null
    },
    {
        "id": "10",
        "question": "Pylon”,
        “asnwer” : null
    },
    {
        "id": “11”,
        “question”: "Banner”,
        “asnwer” : “1”
    },
    {
        "id": "12”,
        "question": “Sport”,
        “answer” : “3”
    },
    {
        "id": “14”,
        “question”: “Matic “,
        “answer” : “1”
    },
    {
        "id": "16”,
        "question": “Cub”,
        “answer” : “3”
    }
]

How do I get array_result like I expected? Please help me, Thank you

Abdul Hoque Nuri
  • 1,105
  • 1
  • 9
  • 18
Ratri
  • 337
  • 1
  • 7
  • 21

2 Answers2

1

Can you try following code:

$sql = "select q.id, q.question, a.aswer from question_table q inner join answer_table a ON q.id = a.id_question”;
$resultPertanyaan = mysqli_query($con, $sql);

while($rowQuery= mysqli_fetch_array($resultPertanyaan)){
   $array_result[]= 
array('id'=>$rowQuery['id’],’question’=>$rowQuery['question'], 'answer' => $rowQuery['aswer']);
}
TigerT
  • 52
  • 3
1

You can use array_map to process each of the questions, looking for an answer in $array_answer for each one:

$questions = json_decode($array_question);
$answers = json_decode($array_answer, true);

$array_result = array_map(function ($v) use ($answers) {
    $v->answer = ($k = array_search($v->id, array_column($answers, 'id_question'))) !== false ? $answers[$k]['answer'] : null;
    return $v;
}, $questions);
print_r(json_encode($array_result, JSON_UNESCAPED_SLASHES));

Output:

[{"id":"8","question":"Shop sign/billboard ","answer":null},
 {"id":"10","question":"Pylon","answer":null},
 {"id":"11","question":"Banner","answer":"1"},
 {"id":"12","question":"Sport","answer":"3"},
 {"id":"14","question":"Matic ","answer":"1"},
 {"id":"16","question":"Cub","answer":"3"}
]

Demo on 3v4l.org

Update

For versions of PHP prior to 5.4, there are a couple of issues with the above code. Firstly array_column was not implemented in PHP until version 5.5.0. Secondly the JSON_UNESCAPED_SLASHES constant and associated functionality wasn't implemented until PHP 5.4.0. We can emulate those with this code:

function my_array_column($array, $column) {
    return array_map(function ($v) use ($column) { return $v[$column]; }, $array);
}

$array_result = array_map(function ($v) use ($answers) {
    $v->answer = ($k = array_search($v->id, my_array_column($answers, 'id_question'))) !== false ? $answers[$k]['answer'] : null;
    return $v;
}, $questions);
echo str_replace('\/', '/', json_encode($array_result));

Demo on 3v4l.org

Nick
  • 138,499
  • 22
  • 57
  • 95
  • why when i add this code to server it show nothing? but if i use local its fine – Ratri Jan 07 '19 at 07:19
  • @Ratri do you have error reporting turned on? Without knowing what error you are seeing it is impossible to answer that question. You can refer to this [question](https://stackoverflow.com/questions/1475297/phps-white-screen-of-death) for some good advice on debugging – Nick Jan 07 '19 at 07:21
  • @Ratri what version of PHP are you running on the server? – Nick Jan 07 '19 at 07:23
  • i use php 5 in my server – Ratri Jan 07 '19 at 07:29
  • 5.3.3 subversion – Ratri Jan 07 '19 at 07:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186285/discussion-between-ratri-and-nick). – Ratri Jan 07 '19 at 07:35