1

I have followed the steps provided in this answer in PHP.

However, I would like to add the items in array_answer in the following way:

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

and the array_question is :

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

and the result (array_result) is :

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

]

In the example I linked, only 1 item from array_answer is entered into array_result, namely answer, now I want to add pi, ca, pic, deadline, new_deadline, reason, and notes. How should I do that? I dont understand, please help me..

Thankyou..

pratiwi
  • 27
  • 6
  • your array question is invalid json because it is littered with typographic/smart quotation marks (`”`), vs. what they should be, neutral/straight quotation marks (`"`) - e.g. `“Cub”` should be `"Cub"` – lovelace Jan 15 '19 at 09:11
  • i have edited my array_question @lovelace – pratiwi Jan 15 '19 at 09:15
  • have you edited all the occurances of `”`, because there are quite a few (cub was just an example) - if the json has invalid chars, your json_decode() will return NULL, which might explain (part of) your issue. – lovelace Jan 15 '19 at 09:18
  • yes i have edited all – pratiwi Jan 15 '19 at 09:30

1 Answers1

0

You can use foreach and arrays to combine them see code bellow :

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

Check live example : click here

HamzaNig
  • 1,019
  • 1
  • 10
  • 33
  • what if I want to display all questions? there is an answer or no answer. If the question has no answer then answer: null – pratiwi Jan 15 '19 at 09:53