Chanaka De Silva
Chanaka De Silva

Reputation: 67

How to create a dynamic JSON array in PHP using multiple for eaches?

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.

Upvotes: 0

Views: 104

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57121

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

Upvotes: 2

Related Questions