通过匹配相同的外键组合2个数组 [英] Combine 2 arrays by matching the same foreign key

查看:65
本文介绍了通过匹配相同的外键组合2个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,分别是问题表和答案表 结构是这样的:

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

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

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

结果是这样的

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结合起来,结果如下:

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

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

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

推荐答案

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

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

输出:

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

在3v4l.org上进行演示

更新

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

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

在3v4l.org上进行演示

这篇关于通过匹配相同的外键组合2个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆