0

I need help with my json output. At present my output is as so:

[
  [
    {
        "ING_SW_CB": "5",
        "SB_SW_CB": "3",
        "NG3_SW_CB": "1",
        "Mould_Close": "4",
        "Leak_Test": "5",
        "ML_Load": "6",
        "Pre_Heat": "3",
        "Dispense": "9",
        "A310": "7",
        ............
    }
  ]
]

I’d like it to be as following with "key" & "value" included in the output.

{"key":"ING_SW_CB", "value": "5"},
{"key":"SB_SW_CB", "value": "3"},
 ..............................

Hers is my PHP script:

$json_response = array();

while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {
    $row_array['ING_SW_CB'] = $row['ING_SW_CB'];
    $row_array['SB_SW_CB'] = $row['SB_SW_CB'];
    $row_array['NG3_SW_CB'] = $row['NG3_SW_CB'];
    $row_array['Mould_Close'] = $row['Mould_Close'];
    $row_array['Leak_Test'] = $row['Leak_Test'];
    $row_array['ML_Load'] = $row['ML_Load'];
    $row_array['Pre_Heat'] = $row['Pre_Heat'];
    $row_array['Dispense'] = $row['Dispense'];
    $row_array['A310'] = $row['A310'];
    $row_array['Gelation'] = $row['Gelation'];
    $row_array['Platen'] = $row['Platen'];
    $row_array['Mainline_Unload'] = $row['Mainline_Unload'];
    $row_array['De_mould'] = $row['De_mould'];
    $row_array['Clean_Up'] = $row['Clean_Up'];
    $row_array['Soda_Blast'] = $row['Soda_Blast'];
    $row_array['Miscellaneous'] = $row['Miscellaneous'];

    //push the values in the array
    array_push($json_response,$row_array);
}

$json_data = json_encode($json_response, JSON_PRETTY_PRINT); 
file_put_contents('your_json_file.json', $json_data); 

I need the keywords "key" & "value" added to the output so I can popuate a D3 chart.

0

3 Answers 3

3

You need to loop on the row's results and build more arrays:

while(...) {
   $temp = array();
   foreach($row as $key => $value) {
       $temp[] = array('key' => $key, 'value' => $value);
   }
   $json_response[] = $temp;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Marc, I'm off to give this a whirl !
For some reason Marc the 'your_json_file.json' is not created. I don't get an error querying but the file is not written.
check the return value of file_put_contents. if it's boolean false, then there's something wrong.
Thank you Marc, this done it for me! I left out a ) in my while loop. Thank you!
1

Loop over the columns:

$json_response = array();
while ($row = mysqli_fetch_array($result2, MYSQLI_ASSOC)) {
    $json_row = array();
    foreach ($row as $key => $value) {
        $json_row[] = array('key' => $key, 'value' => $value);
    }
    $json_response[] = $json_row;
}
$json_data = json_encode($json_response, JSON_PRETTY_PRINT);
file_put_contents('your_json_file.json', $json_data);

6 Comments

Thank yo Barmar, I'm going to alter the code and I'll let you know how I get on, cheers.
For some reason Barmar the 'your_json_file.json' is not created. I don't get an error querying but the file is not written.
Maybe you don't have write permission to the folder. Check your PHP error log for the details.
Hi Barmar, I think there should be an extra ) in the while loop. When I add this the file 'your_json_file.json' is now created but it contains an empty array as following. [ [] ]
I had a couple of other errors: $col should have been $row (didn't you get an Undefined Variable warning for that?), and MYSQL_ASSOC should be MYSQLI_ASSOC (I copied that error from your question).
|
0
$json_response = array();

while ($row = mysqli_fetch_array($result2, MYSQL_ASSOC)) {


//push the values in the array

 foreach($row as $k=>$v){
      $my_array = array("key"=>$k, "value"=>$v);
 }

   array_push($json_response,$my_array);
}



 $json_data = json_encode($json_response, JSON_PRETTY_PRINT); 
 file_put_contents('your_json_file.json', $json_data); 

3 Comments

You're missing the key and value properties.
And you're overwriting $my_array each time through the inner loop.
Hi MrTechie, when I use your method, what is returned for me is all the "Miscellaneous" coulumns with their values. The other columns aren't shown in my external json file.

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.