通过匹配键组合 2 个数组

combine 2 array by matching key

我已按照 PHP 中 中提供的步骤进行操作。

但是,我想通过以下方式添加 array_answer 中的项目:

[
{
    "id": "4c42ff61-148c-11e9-b673-005056be36b2",
    "answer": "1",
    "id_question": "11",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "59a09a34-148c-11e9-b673-005056be36b2",
    "answer": "3",
    "id_question": "12",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "66774e30-148c-11e9-b673-005056be36b2",
    "answer": "3",
    "id_question": "14",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "9469c0e4-148c-11e9-b673-005056be36b2",
    "answer": "1",
    "id_question": "10",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
}
]

array_question 是:

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

结果 (array_result) 是:

[
{
    "id": "10",
    "question": "Shop sign/billboard",
    "answer":"1",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "11",
    "question": "Pylon",
    "answer" : "1",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "12",
    "question": "Banner",
    "answer": "3",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "13",
    "question": "Sport",
    "answer" : null,
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "14",
    "question": "Matic",
    "answer": "3",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "16",
    "question": "Cub",
    "answer" : null,
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
}

]

在我链接的例子中,只有array_answer中的一项被输入到array_result,即answer,现在我想添加picapicdeadlinenew_deadlinereasonnotes。我该怎么做?不懂,求大神指点..

谢谢..

您可以使用 foreacharrays 将它们结合起来,请参阅下面的代码:

      <?php

    $string='[
{
    "id": "4c42ff61-148c-11e9-b673-005056be36b2",
    "answer": "1",
    "id_question": "11",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "59a09a34-148c-11e9-b673-005056be36b2",
    "answer": "3",
    "id_question": "12",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "66774e30-148c-11e9-b673-005056be36b2",
    "answer": "3",
    "id_question": "14",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
},
{
    "id": "9469c0e4-148c-11e9-b673-005056be36b2",
    "answer": "1",
    "id_question": "10",
    "pi": null,
    "ca": null,
    "pic": null,
    "new_deadline": null,
    "reason": null,
    "notes": null,
    "deadline": null
}
]';

    $string2='[
{
    "id": "10",
    "question": "Shop sign/billboard"
},
{
    "id": "11",
    "question": "Pylon"
},
{
    "id": "12",
    "question": "Banner"
},
{
    "id": "13",
    "question": "Sport"
},
{
    "id": "14",
    "question": "Matic"
},
{
    "id": "16",
    "question": "Cub"
}
]';

$json = json_decode($string, true);
$json2 = json_decode($string2, true);

$array0=array();
foreach($json as $key)
{
    $array0[$key['id_question']]=$key;


}


$array1=array();
foreach($json2 as $key)
{

    $array1[$key['id']]=$key['question'];

            if(!isset($array0[$key['id']]))
            {
        $row=array();
        $row["id"]=$key['question'];
        $row["id_question"]=$key['id'];
        $row["answer"]=null;
        $row["pi"]=null;
        $row["ca"]=null;
        $row["pic"]=null;
        $row["new_deadline"]=null;
        $row["reason"]=null;
        $row["notes"]=null;
        $row["deadline"]=null;
        $array0[]=$row;
            }



}
$array2=array();

foreach($array0 as $key)
{

    if(isset($array1[trim($key['id_question'])])){
        $key['question']=$array1[$key['id_question']];
                                                 }

    $key['id']=$key['id_question'];
    unset($key['id_question']);

    $array2[]=$key;




}

// echo
print_r($array2);

// convert to json
$json=json_encode($array2);



    die;

查看实例:click here