0

I need to generate a JSON Object and push it to a JSON object array using PHP. This is my code.

public function getJSONData()
{
    $csv = $this->getCsvData();

    $finalArray = array();

    // First foreach to iterate the data array.
    foreach ($csv["data"] as $key => $value) {

        // Second foreach to iterate the headlines array.
        $newArray = array();
        foreach ($csv["headlines"] as $index => $headline) {
            // $newArray[$key]->$csv["headlines"] = $value;
            // print_r($headline);
            // print_r($value[$index]);
            // echo " ";
            array_push($newArray, array($headline => $value[$index]));
        }
        echo json_encode($newArray);
        echo "<br>";
        array_push($finalArray, json_encode($newArray));
        
    }
    // dd($finalArray);
}

From this code, I'm getting the following response.

[{"ID":12348},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":639.95}] [{"ID":12348},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":509.95}] [{"ID":12348},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":349.95}] [{"ID":12349},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":639.95}] [{"ID":12349},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":509.95}] [{"ID":12349},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":349.95}] [{"ID":12350},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":639.95}] [{"ID":12350},{"Status":1},{"Manufacturere_ID":101},{"Famiy_matchcode":"101-iphone-11"},{"Name":"iPhone 11"},{"Price":509.95}]

But it is not a valid JSON and I really need to get an output like this:

[{"ID":12348,"Status":1,"Manufacturere_ID":101,"Famiy_matchcode":"101-iphone-11","Name":"iPhone 11","Price":639.95}, {"ID":12348,"Status":1,"Manufacturere_ID":101,"Famiy_matchcode":"101-iphone-11","Name":"iPhone 11","Price":509.95}]

This is a standard JSON object array.

In my code, I'm using array_push($newArray, array($headline => $value[$index])); to generate the array and take it as JSON.

Please help me on this.

2
  • Generate the full array first, and only then use json_encode on that result. Commented Oct 21, 2020 at 11:45
  • @Jonnix Can you please provide some more explanation please. You are in expert level and I'm in beginner level. :-) Commented Oct 21, 2020 at 12:04

1 Answer 1

2

You seem to be creating arrays and adding json data in several places, build the data as a normal array and then encode the result...

// Foreach to iterate the data array.
foreach ($csv["data"] as $key => $value) {
    // Combine headers with data and add to final result
    $finalArray[] = array_combine($csv["headlines"], $value);
    
}
echo json_encode($finalArray);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm checking your answers now and thank you in advance. I will tell you the results. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.