Transform Json into array list

Asked

Viewed 84 times

2

I need to transform a result into JSON, arrays so I can create a list...

See my code:

$headers = array();
$headers[] = 'Content-Type: application/json';

$ch = curl_init();
curl_setopt_array($ch, [

    // Produtos
    CURLOPT_URL            => 'http://www.minhaloja.com.br/api.php/produtos',


    CURLOPT_HTTPHEADER     => $headers,
    CURLOPT_CUSTOMREQUEST  => 'GET',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYHOST => false,
    CURLOPT_SSL_VERIFYPEER => false
]);
$out = curl_exec($ch);
curl_close($ch);
print($out);

CURRENT RESULT:

{  "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"]]}}

I want you to be like this:

  • Product Quantity Price

    • Headset 1 100
    • Mascara 88 74
  • If you want more details in my reply, consider [Edit] your question and add the contents of the variable $out. ;)

  • 1

    @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.

  • 1

    Thank you I’ll wait then! Hugging!

  • @Lipespry, my friend, can you tell me?

  • Updated answer. But, man, I simply "applied" what I had answered to your JSON. How hard it is to do that?

1 answer

2


"- 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'].

  • Friend, I did so to return all products: $array_assoc = json_decode($out, true); //print_r($array_assoc); echo 'Product: '$array_assoc['product']; foreach($array_assoc['product']['Records'] as $key => $value) {&#echo $; } But returned error:arning: Invalid argument supplied for foreach() in /home/.... e To display 1 product, I did as you said: echo 'Product: '.$array_assoc['product']['Records'][0][1]; Mas!

  • @I’m glad you’re getting it! Hehe

  • Friend, I sent the question without finishing writing because the enter goes automatic, look what I wrote.. hug!

  • @liPestpry if you know how to help...

  • @I can help you. But this question is already over the limit. It’s already a new problem. There is some contact information in my profile. Just call it something more dedicated.

  • If you think this is over the limit all right. What you answered me was what I knew too, I need to solve anyway. In my question there was already this issue of printing in a list! I solve alone. hug

  • @Don’t get me wrong, man. I didn’t deny you help. I told you to get in touch that I would help you. In the question you want to know how to transform a json into an array. Then we still apply my answer to your json. Now you are having trouble making a loop in the array. This is a separate issue, in my opinion. If you think better, you can create a chat here also. I’ll help you. It can even be via Teamviewer. But here is public to exchange this kind of information. Until!

  • It worked well friend! I ended up giving a name for example of a column here, and then does not replace because the name was almost the same. Now it worked. Thank you very much!

  • @Somebody tell me, mate! ✌

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.