I am calling data from an API like this:
$curl = curl_init();
//adding fields to the curl object to enter the site
curl_setopt($curl, CURLOPT_URL, $my_url);
curl
Use json_decode()
. If you want to view the php object so you can better understand how to use it, try
echo '<pre>';
echo print_r(json_decode($jsonStr, true));
echo '</pre>';
You can use json_decode() method
Please refer this link http://php.net/manual/en/function.json-decode.php
Not sure why is not working for you. When the json string is in the code it does work.
<?php
$json = '[{"id":1,"name":"Books","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"200.0000"}],"tax":[]},{"id":2,"name":"pencil","description":null,"reference":null,"status":"active","category":{"id":"5048","name":"Ventas"},"price":[{"idPriceList":"1","name":"General","type":"amount","price":"5000.0000"}],"tax":[]}]';
$arr = json_decode($json, true);
$id = $arr[0]['id'];
$name = $arr[0]['name'];
print("$id $name\n");
Ouput:
1 Books
Btw my version
PHP 5.6.30 (cli) (built: Aug 8 2017 12:20:45)