-4

I have retrieved some data like this:

object(stdClass)[1]
  public 'status' => string 'ok' (length=2)
  public 'data' => 
    object(stdClass)[3]
      public 'sort_data' => 
        object(stdClass)[2]
          public 'sort_column' => string 'order_item_id' (length=13)
          public 'sort_order' => string 'asc' (length=3)
      public 'pager' => 
        object(stdClass)[4]
          public 'page' => int 1
          public 'item_per_page' => int 50
          public 'total_page' => int 1
          public 'total_rows' => int 21
      public 'form_data' => 
        array (size=0)
          empty
      public 'items' => 
        array (size=21)
          0 => 
            object(stdClass)[5]
              public 'order_item_id' => int 323360064
              public 'order_id' => int 111179028
              public 'variant' => 
                object(stdClass)[6]
                  public 'id' => int 17586275
                  public 'seller_id' => int 186764
                  public 'site' => string 'digikala' (length=8)
                  public 'is_active' => boolean true
                  public 'is_archived' => boolean false
                  public 'title' => string 'Mug' (length=115)
                  public 'product' => 
                    object(stdClass)[7]
                      public 'id' => int 3634323
                      public 'category_id' => int 6289
                      public 'title' => string 'Mug model series' (length=40)
                      public 'shipping_nature_id' => int 1
                      ...

And the code behind this goes here:

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'site_url',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json, application/json;charset=UTF-8',
    'Authorization: {{token}}'
  ),
));

$response = curl_exec($curl);

var_dump(json_decode($response));

Now I need to access each of these values separately. For example I want to get public 'title' => string 'Mug model series'. How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    it seems you censored the var_dump output and removed some crucial data for correct access in the process. in any case, show us the var_export() output, not the var_dump() output. in the data you provided, there's no way the first 3 lines really are ``` object(stdClass)[1] public 'status' => string 'ok' (length=2) public 'data' => ``` if the 2nd and 3rd lines are correct, there's no way the first line would end with `[1]`, it should have ended with `[2]` or some higher digit, the number of members in the object. (which is at at least 2 given line 2 is #1 and line 3 is #2). CAN-NOT-REPRODUCE. – hanshenrik Jun 25 '21 at 11:55

5 Answers5

5

You can tell json_decode() to return an array instead of an object:

$response = json_decode($response,true);
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
Emepese
  • 419
  • 3
  • 15
0

Now I need to access each of these values separately. For example I want to get public 'title' => string 'Mug model series'

...
$object = json_decode($response);

echo $object->data->items[0]->variant->product->title;
// Mug model series

[0] means the first item in the array, so if you want to loop it:

foreach ($object->data->items as $item) 
  echo $item->variant->product->title;
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Notice: Trying to get property 'data' of non-object –  Jun 25 '21 at 04:00
  • 1
    @tejoslaeslio that's on you, it seems you censored the var_dump output and removed some crucial data for correct access in the process. in any case, show us the var_export() output, not the var_dump() output. in the data you provided, there's no way the first 3 lines really are ``` object(stdClass)[1] public 'status' => string 'ok' (length=2) public 'data' => ``` you removed something. if the 2nd and 3rd lines are correct, there's no way the first line would end with `[1]`, it should have ended with `[2]` or some higher digit. – hanshenrik Jun 25 '21 at 11:48
  • @tejoslaeslio answer is correct from the data provided, you need to research *php access nested object property* its something you would need to know in and out, essentially you traverse through using `->`, if you want arrays then decode as an array, like the answer above and access as associative `echo $object['data']['items'][0]['variant']['product']['title'];` – Lawrence Cherone Jun 25 '21 at 13:30
  • or plop it in [ArrayObject](https://www.php.net/manual/en/class.arrayobject.php) and have best of both – Lawrence Cherone Jun 25 '21 at 13:31
0

first of all you need to convert the object to array by using

$response = json_decode($response, true);

after decoding the response and getting array instead of object you can access the data btw typing the following

echo $response->data->items[0]->order_id;
// output : 111179028

and let's say you have to display each data in table or something you need a foreach loop for that like this :

foreach($response->data->items as $item){
?>
 <h1>
  <?php 
   echo $item->variant->seller_id; 
  ?>
 </h1>
 <h2>
  <?php
   echo $item->variant->product->title
  ?>
 </h2>
<?php
}
?>

and you can modify the view of the page or show the information you want with this if you need more information you can visit the links below

https://www.php.net/manual/en/function.json-decode.php

https://www.w3schools.com/php/php_json.asp

i hope you found this answer useful

0

try this

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

Decodes a JSON string

var_dump(json_decode($response))
$json= json_decode($json,true);
Rizwan
  • 3,741
  • 2
  • 25
  • 22
0

Note: For future it would have been easier if you just printed data like this.

echo "<pre>";
print_r($response);

Now that's out of the way, what you are receiving seems like the data below taking into account you do json_decode($response, true) (True as second param gives you associative array).

[
 "parentStdClass" => [  // (Your question starts from here as stdclass has index 1 probably array)
     ....
     "data" => [
           ...unrelatedData removed form this eg,
           "items" => [ // you have 21 items
             .... 20 items,
             [

                 ...,
                 "variant" => [
                    ...,
                    "title" => "Some title", //variant title

                    "product" => [ // 7 items are here
                       ...,
                       [
                           'title' => 'Mug model series',
                       ] 
                    ]
                 ]
             ],

   
           ],
     ]
 ]
]  

Now to get the title of first product you need to do.

$response = json_decode($response, true);
// Put what the key name is instead of 0 if it's associative array
$questionStartingArray = $response[0];

$data = $questionStartingArray['data'];
// Give you all items
$items = $questionStartingArray['items'];

// Gives you first item variant & title
$variant = $items[0]['variant'];
$variantTitle = $variant['title'];

// Gives you first variants product  & product title
$product = $variant['product'];
$productTitle = $product[0]['title'];
$allProductTitleInsideVariant = array_reduce($product , function($carry, $item){
   $carry[] = $item['title'];
   return $carry;
},[]);

if you want all the product title inside of a item, then fetch it though the loop. eg:

$productTitle = [];
$variantTitle = [];
foreach($items as $item) {
   foreach($item['variant'] as $variantItem) {
      $variantTitle[] = $variantItem['title'];
      foreach($variantItem['product'] as $productItem) {
         $productTitle[] = $productItem['title'];
      } 

   } 
}

echo "<pre>";
print_r($productTitle);
print_r($variantTitle);

And you will get all the product title inside of a items array.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rajesh Paudel
  • 1,117
  • 8
  • 19