通过匹配相同的外键组合2个数组

时间:2019-01-07 04:03:24

标签: php arrays json array-merge

我有2张桌子,分别是问题表和答案表 结构是这样的: enter image description here

我有一个像这样使用php从question_table和answer_table获得的JSON数组。

// 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']);
}

结果是这样的

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”
    }
]

获得答案

$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']);
}

和这样的结果

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"
    }
]

我想将array_answer与array_question结合起来,结果如下:

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”
    }
]

如何获得预期的array_result?请帮助我,谢谢

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码吗?

$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']);
}

答案 1 :(得分:1)

您可以使用array_map处理每个问题,并在$array_answer中为每个问题寻找答案:

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

输出:

[{"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

更新

对于5.4之前的PHP版本,上述代码存在两个问题。首先,直到版本5.5.0才在PHP中实现array_column。其次,直到PHP 5.4.0才实现JSON_UNESCAPED_SLASHES常量和相关功能。我们可以使用以下代码来模拟它们:

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