"- transform a result into JSON, arrays"
Suffice parse return of the Curl:
json_decode($out, true);
The second argument true
defines that the function json_decode
return as array associative, rather than an object of the class \stdClass
.
That is to say:
<?php
$json = '{"chave1":"valor1", "chave2":"valor2"}';
$array_assoc = json_decode($json, true);
print_r($array_assoc);
/* Retorna:
* Array
* (
* [chave1] => valor1,
* [chave2] => valor2
* )
******************************************/
Recommended reading: PHP: json_decode
@Edit:
"- But what I want is to print legibly as if it were the result on a table! Because using my code or yours is still unreadable for the end user."
Well, after "decoding" the string JSON, just display the values. You already have the list you wanted:
<?php
$out = '{ "product":{"columns":["product_id","model","sku","upc","ean","jan","isbn","mpn","location","quantity","stock_status_id","image","manufacturer_id","shipping","price","points","tax_class_id","date_available","weight","weight_class_id","length","width","height","length_class_id","subtract","minimum","sort_order","status","viewed","date_added","date_modified"],"records":[[65,"Colete deslize preto","","","","","","","",1,6,"catalog\/WhatsApp Image 2017-06-01 at 15.40.45 (2).jpeg",8,1,"219.0000",0,0,"2017-06-01","1.00000000",1,"34.00000000","24.00000000","32.00000000",1,1,1,1,1,233,"2017-06-01 20:03:40","2017-06-19 17:51:27"]]}}';
$array_assoc = json_decode($out, true);
print_r($array_assoc);
/* Retorna:
Array
(
[product] => Array
(
[columns] => Array
(
[0] => product_id
[1] => model
[2] => sku
[3] => upc
[4] => ean
[5] => jan
[6] => isbn
[7] => mpn
[8] => location
[9] => quantity
[10] => stock_status_id
[11] => image
[12] => manufacturer_id
[13] => shipping
[14] => price
[15] => points
[16] => tax_class_id
[17] => date_available
[18] => weight
[19] => weight_class_id
[20] => length
[21] => width
[22] => height
[23] => length_class_id
[24] => subtract
[25] => minimum
[26] => sort_order
[27] => status
[28] => viewed
[29] => date_added
[30] => date_modified
)
[records] => Array
(
[0] => Array
(
[0] => 65
[1] => Colete deslize preto
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] => 1
[10] => 6
[11] => catalog/WhatsApp Image 2017-06-01 at 15.40.45 (2).jpeg
[12] => 8
[13] => 1
[14] => 219.0000
[15] => 0
[16] => 0
[17] => 2017-06-01
[18] => 1.00000000
[19] => 1
[20] => 34.00000000
[21] => 24.00000000
[22] => 32.00000000
[23] => 1
[24] => 1
[25] => 1
[26] => 1
[27] => 1
[28] => 233
[29] => 2017-06-01 20:03:40
[30] => 2017-06-19 17:51:27
)
)
)
*/
To return the specific values, just follow the trace of the desired value:
echo 'Produto: '.$array_assoc['product']['records'][0][1]; // Produto: Colete deslize preto
echo 'Quantidade: '.$array_assoc['product']['records'][0][9]; // Quantidade: 1
echo 'Preço: '.$array_assoc['product']['records'][0][14]; // Preço: 219.0000
When more than one product returns, just make one loop foreach
in the key ['product']['records']
.
If you want more details in my reply, consider [Edit] your question and add the contents of the variable
$out
. ;)– LipESprY
@Lipespry I edited the question. But what I want is to print legibly as if it were the result in a table! Because using my code or yours is still unreadable for the end user.
– Alh
Thank you I’ll wait then! Hugging!
– Alh
@Lipespry, my friend, can you tell me?
– Alh
Updated answer. But, man, I simply "applied" what I had answered to your JSON. How hard it is to do that?
– LipESprY