0

I thought it would be simple but of course I'm at a roadblock, I'm new to handling JSON data but here is the info.

$new_item = '[2554560000, 18.26]';
$load = file_get_contents("json/graphlist.json");
$data = json_decode($load, true);
var_dump($data);
echo "<br /><br />";
echo ($data['product1']);
echo "<br /><br />";
echo json_encode($data);


$save = file_put_contents('json/graphlist.json', json_encode($data));

JSON Data:

{"product1":[[243500000,17.85],[245500000,14.65],[248500000,16.22]]}

when I var_dump the decoded data it comes out as:

array(1) { ["product1"]=> array(3) { [0]=> array(2) { [0]=> int(243500000) [1]=> float(17.85) } [1]=> array(2) { [0]=> int(245500000) [1]=> float(14.65) } [2]=> array(2) { [0]=> int(248500000) [1]=> float(16.22) } } }

yet when I echo the product1 array it only shows:

Array

So my question is how do I access the data within the second Array. Then append the data from $new_item to the existing strings to be saved back to the JSON file. I've looked everywhere yet I can't find anyone with similar examples or errors.

2
  • You can access the elements of the array by echo $data['product1'][0][0]; or view the whole array by print_r $data['product1']; Commented Feb 20, 2013 at 19:03
  • Thank you also for your input on the print_r code, that also worked well in helping me to solve the coding. btw print_r I realized has to have its code blanketed with () Commented Feb 20, 2013 at 20:07

3 Answers 3

2

You probably want to do

echo($data["product1"][0][0]);

instead, to get the first number out.

1
  • that definitely solves my issues when I was wanting to look at the data, hopefully this will help me figure out how to append the new data to it. Commented Feb 20, 2013 at 19:26
0

You can't call echo on an array, it will correctly just display Array.

If you want to access the data inside it, you can do something like:

echo $data['product1'][0][0] // echoes '243500000'

0

You need to use print_r() to display the entire array.

 print_r($data['product1']);

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.