Home > front end >  How to Convert PHP Array to a specific JSON Output Array
How to Convert PHP Array to a specific JSON Output Array

Time:10-13

This is my array that I wants to convert


Array
(
   [0] => stdClass Object
       (
           [Item1] => 10/12/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [1] => stdClass Object
       (
           [Item1] => 10/11/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [2] => stdClass Object
       (
           [Item1] => 10/10/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [3] => stdClass Object
       (
           [Item1] => 10/09/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [4] => stdClass Object
       (
           [Item1] => 10/08/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [5] => stdClass Object
       (
           [Item1] => 10/07/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [6] => stdClass Object
       (
           [Item1] => 10/06/2021
           [Item2] => Array
               (
                   [0] => 3729195
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [7] => stdClass Object
       (
           [Item1] => 10/05/2021
           [Item2] => Array
               (
                   [0] => 7458390
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [8] => stdClass Object
       (
           [Item1] => 10/04/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

   [9] => stdClass Object
       (
           [Item1] => 10/03/2021
           [Item2] => Array
               (
                   [0] => 0
                   [1] => 0
                   [2] => 0
                   [3] => 0
               )

       )

)

I wants this array output like this -

[{name:"", data:[0,0,0,0,0,0,3729195,7458390,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]},
{name:"", data:[0,0,0,0,0,0,0,0,0,0]}]

The problem is that the first element of [Item2][0] input array is the first elements of each 4 rows of data:[0] and second element of [Item2][1] is second column of each data set of data[1] elements of all 4 rows, I hope you understand what I mean.

And the dates should be in another separate list like this -


['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021'],

I have already done some tests and here is what I did so far -

$ChartDataNew  = stripcslashes(trim(json_encode($jobs->charts_last10days), "\""));

$replace1 = str_replace('Item1', name, $ChartDataNew);
$replace2 = str_replace('Item2', data, $replace1);
print_r($replace2);

The above code is giving me output of an JSON object

[{"name":"10/12/2021","data":[0,0,0,0]},{"name":"10/11/2021","data":[0,0,0,0]},{"name":"10/10/2021","data":[0,0,0,0]},{"name":"10/09/2021","data":[0,0,0,0]},{"name":"10/08/2021","data":[0,0,0,0]},{"name":"10/07/2021","data":[0,0,0,0]},{"name":"10/06/2021","data":[3729195,0,0,0]},{"name":"10/05/2021","data":[7458390,0,0,0]},{"name":"10/04/2021","data":[0,0,0,0]},{"name":"10/03/2021","data":[0,0,0,0]}]

This JSON seems ok, but its not correct. First of all I wants to make the JSON output as given above without "" and name should be name:"". When you see the data in output of my json object is not matching to date in input array.

To get the list of dates I have done this and its fine -

$chartDataSeries = array();
$chartDateLabels = array();
$chartSeriesList = array();
foreach($jobs->charts_last10days as $chartData){
array_push($chartDateLabels, "'".$chartData->Item1."'");
array_push($chartDataSeries, $chartData->Item2);         
           
} 

$chartDateLabels1 = implode(', ', ($chartDateLabels));
echo ($chartDateLabels1);

Its giving me the correct output for list of dates, see

['10/12/2021', '10/11/2021', '10/10/2021', '10/09/2021', '10/08/2021', '10/07/2021', '10/06/2021', '10/05/2021', '10/04/2021', '10/03/2021']

please let me know what I am doing wrong or help me out how to do that, may be write some functions or any other way to do this.

Thanks in Advance

CodePudding user response:

There's probably a more elegant solution, but the easiest way I could think of was using iterating through each element, and then constructing another array:

$resultArr = [];

// Iterate over each element of the original array
foreach ($dataArr as $key => $arr) {
    // Set the date
    $datesArr[$key] = $arr->Item1;

    // Iterate over each element of Item2
    foreach ($arr->Item2 as $i => $v) {
        // Set 'data' on the resulting array item $i to the value
        //  using the $key from the outer loop as the index for the inner data array
        $resultArr[$i]['data'][$key] = $v;

        // Set the name
        $resultArr[$i]['name'] = '';
    }
}

// Convert from an array to JSON
echo json_encode($resultArr) . PHP_EOL;

// The dates are in $datesArr
echo json_encode($datesArr) . PHP_EOL;

$dataArr is the variable where your data is currently stored. After running this loop, $resultArr will be an array, of which each element is another array containing the data in the right format, and $datesArr is the dates from the data in their own array.

  • Related