通过匹配键将数组中的新项目添加到组合数组

add new items from the array to the combined array by matching key

我想通过匹配键来组合2个数组,我在这里试过https://3v4l.org/Jp9Jf我已经成功地组合了两个数组,但是我想将从$string2获得的项目添加到数组result

有一个

$string2='[
{
    "id": "38",
    "question": "Kebersihan Meja Counter dan Meja Fincoy ",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "39",
    "question": "Seragam Sales Counter / Salesman* ",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "40",
    "question": "Kerapihan Sales Counter",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "42",
    "question": "Sales Guide",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "43",
    "question": "Product Card",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "44",
    "question": "Ketersediaan Buku Tamu",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "45",
    "question": "Ketersediaan Price List",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "46",
    "question": "Ketersedian Rak Brosur ",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
},
{
    "id": "47",
    "question": "Ketersediaan Flyer*/brosur\t",
    "type": "NON DOS",
    "last_root": "1",
    "exist_good": "1",
    "exist_not_good": "0",
    "not_exist": "1",
    "n_a": "0"
}
]

我希望我的结果是这样的

[{"id":"7ac648ce-18aa-11e9-b673-005056be36b2","answer":"1","id_question":"39","pi":"very good","ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null,"type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0"},{"id":"8653ef3d-18aa-11e9-b673-005056be36b2","answer":"3","id_question":"40","pi":"no problem","ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null,"type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0"},{"id":"Kebersihan Meja Counter dan Meja Fincoy ","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"38","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Sales Guide","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"42","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Product Card","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"43","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Ketersediaan Buku Tamu","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"44","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Ketersediaan Price List","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"45","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Ketersedian Rak Brosur ","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"46","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null},{"id":"Ketersediaan Flyer*\/brosur\t","type":"NON DOS","last_root":"1","exist_good":"1","exist_not_good":"0","not_exist":"1","n_a":"0","id_question":"47","answer":null,"pi":null,"ca":null,"pic":null,"new_deadline":null,"reason":null,"notes":null,"deadline":null}]

但我无法从 [= 添加项目 typelast_rootexist_goodexist_not_goodnot_existn_a 18=] 到数组结果。如何添加这些项目?请有人帮助我,我的 php 版本是 5.3.3

为了简化您的代码,我已尝试以尽可能少的步骤完成。这样可以节省多次移动数据(代码中的注释)...

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

// Create $result with an array indexed by id_question
$result = array_column($json, null, "id_question");

foreach($json2 as $key)
{
    // If question doesn't exist
    if(!isset($result[$key['id']]))
    {
        // Set values from new question
        $row=$key;
        $row["id_question"]=$key['id'];
        $row["id"]=$key['question'];
        // Remove this element as not needed in new array
        unset ( $row['question']);
        $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;
        // Add to result
        $result[]=$row;
    }
    else
    {
        // Question is already present, just add in new details
        $result[$key['id']]["type"]=$key['type'];
        $result[$key['id']]["last_root"]=$key['last_root'];
        $result[$key['id']]["exist_good"]=$key['exist_good'];
        $result[$key['id']]["exist_not_good"]=$key['exist_not_good'];
        $result[$key['id']]["not_exist"]=$key['not_exist'];
        $result[$key['id']]["n_a"]=$key['n_a'];
    }
}

// Use array_values() to remove keys (the question ID) and encode result
$json=json_encode(array_values($result));
print_r($json);

这个解决方案比它可以写的更冗长,但这种编码设计的好处是易于维护和子数组元素的顺序。如果你仔细观察,你会发现输出中的每个元素总是以相同的顺序排列,无论它来自哪个输入数组。

因为此解决方案仅使用语言结构 (foreach()),所以它适用于每个 php 版本。

代码:(Demo)

foreach ($array1 as $row) {
    $result[$row['id_question']]['id']             = $row['id'];
    $result[$row['id_question']]['id_question']    = $row['id_question'];
    $result[$row['id_question']]['type']           = null;
    $result[$row['id_question']]['answer']         = $row['answer'];
    $result[$row['id_question']]['pi']             = $row['pi'];
    $result[$row['id_question']]['ca']             = $row['ca'];
    $result[$row['id_question']]['pic']            = $row['pic'];
    $result[$row['id_question']]['deadline']       = $row['deadline'];
    $result[$row['id_question']]['new_deadline']   = $row['new_deadline'];
    $result[$row['id_question']]['reason']         = $row['reason'];
    $result[$row['id_question']]['notes']          = $row['notes'];
    $result[$row['id_question']]['last_root']      = null;
    $result[$row['id_question']]['exist_good']     = null;
    $result[$row['id_question']]['exist_not_good'] = null;
    $result[$row['id_question']]['not_exist']      = null;
    $result[$row['id_question']]['n_a']            = null;
}

foreach ($array2 as $row) {
    if (isset($result[$row['id']])) {         // default elements already declared, just overwrite the nulls
        // $row['question'] is omitted from data
        $result[$row['id']]['type']           = $row['type'];
        $result[$row['id']]['last_root']      = $row['last_root'];
        $result[$row['id']]['exist_good']     = $row['exist_good'];
        $result[$row['id']]['exist_not_good'] = $row['exist_not_good'];
        $result[$row['id']]['not_exist']      = $row['not_exist'];
        $result[$row['id']]['n_a']            = $row['n_a'];
    } else {
        $result[$row['id']]['id']             = $row['question'];  // no id, so use question
        $result[$row['id']]['id_question']    = null;
        $result[$row['id']]['type']           = $row['type'];
        $result[$row['id']]['answer']         = null;
        $result[$row['id']]['pi']             = null;
        $result[$row['id']]['ca']             = null;
        $result[$row['id']]['pic']            = null;
        $result[$row['id']]['deadline']       = null;
        $result[$row['id']]['new_deadline']   = null;
        $result[$row['id']]['reason']         = null;
        $result[$row['id']]['notes']          = null;
        $result[$row['id']]['last_root']      = $row['last_root'];
        $result[$row['id']]['exist_good']     = $row['exist_good'];
        $result[$row['id']]['exist_not_good'] = $row['exist_not_good'];
        $result[$row['id']]['not_exist']      = $row['not_exist'];
        $result[$row['id']]['n_a']            = $row['n_a'];
    }
}
var_export($result);

输出:

array (
  39 => 
  array (
    'id' => '7ac648ce-18aa-11e9-b673-005056be36b2',
    'id_question' => '39',
    'type' => 'NON DOS',
    'answer' => '1',
    'pi' => 'very good',
    'ca' => NULL,
    'pic' => NULL,
    'deadline' => NULL,
    'new_deadline' => NULL,
    'reason' => NULL,
    'notes' => NULL,
    'last_root' => '1',
    'exist_good' => '1',
    'exist_not_good' => '0',
    'not_exist' => '1',
    'n_a' => '0',
  ),
  40 => 
  array (
    'id' => '8653ef3d-18aa-11e9-b673-005056be36b2',
    'id_question' => '40',
    'type' => 'NON DOS',
    'answer' => '3',
    'pi' => 'no problem',
    'ca' => NULL,
    'pic' => NULL,
    'deadline' => NULL,
    'new_deadline' => NULL,
    'reason' => NULL,
    'notes' => NULL,
    'last_root' => '1',
    'exist_good' => '1',
    'exist_not_good' => '0',
    'not_exist' => '1',
    'n_a' => '0',
  ),
  38 => 
  array (
    'id' => 'Kebersihan Meja Counter dan Meja Fincoy ',
    'id_question' => NULL,
    'type' => 'NON DOS',
    'answer' => NULL,
    'pi' => NULL,
    'ca' => NULL,
    'pic' => NULL,
    'deadline' => NULL,
    'new_deadline' => NULL,
    'reason' => NULL,
    'notes' => NULL,
    'last_root' => '1',
    'exist_good' => '1',
    'exist_not_good' => '0',
    'not_exist' => '1',
    'n_a' => '0',
  ),
  42 => 
  array (
    'id' => 'Sales Guide',
    'id_question' => NULL,
    'type' => 'NON DOS',
    'answer' => NULL,
    'pi' => NULL,
    'ca' => NULL,
    'pic' => NULL,
    'deadline' => NULL,
    'new_deadline' => NULL,
    'reason' => NULL,
    'notes' => NULL,
    'last_root' => '1',
    'exist_good' => '1',
    'exist_not_good' => '0',
    'not_exist' => '1',
    'n_a' => '0',
  ),
...
)

从这里,您可以使用 ksort()id 对数据行排序,array_values() 重新索引输出,或转换为 json json_encode().