Skip to main content

PHP stdClass Object within array

Dhimant

I have no idea what the stdClass Object is. I googled it but couldn’t find anything really informative about the stdClass.A Few articles on stackoverflow explains the basic idea behind stdClass but that’s it. I haven’t searched the official documentation though.

Anyways, this article is not about stdClass Object. It is about retrieving values from array whose members are of type stdClass.

Here is a link to official php array documentation : http://php.net/manual/en/language.types.array.php

Now if you print_r the simple multidimensional array It would look something like this.

Example array :

$array = array(
   "foo" => "bar",
   42    => 24,
   "multi" => array(
        "dimensional" => array(
            "array" => "foo"
      )
  )
);

print_r($array) of the above will give the following result.

Array ( [foo] => bar [42] => 24 
[multi] => Array ( [dimensional] => Array ( [array] => foo ) ) )

Now if you want to access the value from “array” you can do that fairly easily with following code.

echo $array["multi"]["dimensional"]["array"];

That is simple enough.

But sometimes you may also encounter an array which would look like this.

Array ( [0] => stdClass Object ( [index] => 1 [title] => This is title 
[coupon] => This is coupon [link] => http://www.google.com )
[1] => stdClass Object ( [index] => 2 [title] => This is title 2
[coupon] => This is coupon 2 [link] => http://www.yahoo.com ) )

Now if you look at the array elements in the first dimension,they are of type stdClass Object. You can not just retrieve the value like we did before in the example.

Syntax to retrieve the values from the array similar to shown above is $array[0]->KEY.

echo $array[0]->link;
echo $array[1]->link;

We can also loop over array objects like this

foreach ($array as $key => $object)
{
echo $object->index;
echo $object->title;
echo $object->coupon;
echo $object->link;
}

Complete Example :

 <?php

$encoded_json = '[{"index":1, "title":"This is title", "coupon":"This is coupon", "link":"http://www.google.com" }, {"index":2, "title":"This is title 2", "coupon":"This is coupon 2", "link":"http://www.yahoo.com"} ]';

$decode = json_decode($encoded_json);
print_r($decode);

// Single Values Retrival
echo "<p>";
echo $decode[0]->index ."<p>";
echo $decode[0]->title;
echo "<p>";
echo $decode[1]->index."<p>";
echo $decode[1]->title;
foreach ($decode as $key => $object)
{
echo "<p>";
echo $object->index;
echo "<p>";
echo $object->title;
echo "<p>";
echo $object->coupon;
echo "<p>";
echo $object->link;
echo "<p>";
}
?>

Output :

Array ( [0] => stdClass Object ( [index] => 1 [title] => This is title [coupon] => This is coupon [link] => http://www.google.com ) [1] => stdClass Object ( [index] => 2 [title] => This is title 2 [coupon] => This is coupon 2 [link] => http://www.yahoo.com ) )

1

This is title

2

This is title 2

1

This is title

This is coupon

http://www.google.com

 

2

This is title 2

This is coupon 2

http://www.yahoo.com