0

I want to combine 2 array by matching the key, I have tried in here https://3v4l.org/Jp9Jf I have successfully combined the two arrays, but I want to add the items obtained from $string2 to array result

There is a

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

i want my result is like this

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

but I can't add item type, last_root, exist_good, exist_not_good, not_exist and n_a from $string2 into array result. How can add these items? Please some one help me, and my php version is 5.3.3

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
pratiwi
  • 27
  • 6
  • Can you please make **short** version of your example? and your desire output? – dWinder Jan 20 '19 at 08:11
  • i have edited my question, please check @DavidWinder – pratiwi Jan 20 '19 at 09:23
  • Nigel answer seem fine (I guess you can implement `array_column` by yourself). Notice that in your code (the link) you do not take care of the case the question is already there - add else to the `if(!isset...` with adding the question missing data as Nigel answer – dWinder Jan 20 '19 at 11:38

2 Answers2

0

To simplify your code, I've tried to do it in as few steps as possible. This saves moving data around several times (comments in code)...

$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);
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • array_column is not supported prior to PHP 5.5.0. and my php version is 5.3.3 @Nigel Ren – pratiwi Jan 20 '19 at 09:21
  • @pratiwi take a look at [this answer](https://stackoverflow.com/questions/54068426/combine-2-arrays-by-matching-the-same-foreign-key/54068598#54068598) it has an implementation of `array_column` for versions of PHP prior to 5.5 (see the **Update** section) – Nick Jan 20 '19 at 09:28
  • yes i have check the link, I have tried this method, but I cannot add items other than the example @Nick – pratiwi Jan 20 '19 at 09:35
0

This solution is more verbose than it could be written, but the benefit to this coding design is ease of maintainability and order of subarray elements. If you look closely, you will see that every element in the output is always in the same order regardless of which input array it came from.

Because this solution only uses the language construct (foreach()), it will work in EVERY php version.

Code: (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);

Output:

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',
  ),
...
)

From here, you can use ksort() to order the rows of data by id, array_values() to re-index the output, or convert to json with json_encode().

mickmackusa
  • 43,625
  • 12
  • 83
  • 136