-1

I know there are lots of question regarding this but i want one line solution for this so can anyone tell me how can i do this. I want to get the specific key value in array. This is the array i am getting

$size = json_decode($pid[0]->dtls);
echo "<pre>";
print_r($size);
echo "</pre>";

stdClass Object
(
    [size] => 0.5
    [qty] => 1
    [flavor] => choclate syrup
)

and this is what i am trying to do to echo it.

echo $size = $size["size"];  

But i am getting this error

Fatal error: Cannot use object of type stdClass as array

what i want to echo is 0.5

Jayant Rawat
  • 49
  • 1
  • 13

2 Answers2

1

Try this :

$size = json_decode($pid[0]->dtls, TRUE);
echo "<pre>";
print_r($size);
echo "</pre>";
echo $size = $size["size"];

or you can get object value like this echo $size = $size->size;

Muthu17
  • 1,481
  • 12
  • 20
  • Thanks for your answer but chetan had already mentioned that so his answer works for me. By this is also the correct one thanks for your time mate – Jayant Rawat Mar 28 '17 at 09:21
0

You are not supposed to print object in the form of array.

To access array you have to use like the way you mentioned.

To access the object you have to use like

$size->size

Read the below document to know more about objects in php

http://php.net/manual/en/language.types.object.php

Jagadeesh
  • 734
  • 6
  • 26