0

I'm trying to print a value from a object array, but I'm facing some problems. I want to echo in php the value of [title] but i am not getting it!

stdClass Object (
    [result] => success
    [records] => stdClass Object (
        [500272328] => stdClass Object (
            [nif] => 500272328
            [pc4] => 2775
            [pc3] => 372
            [seo_url] => solnave-restaurantes-e-alimentacao-s-a
            [title] => Solnave - Restaurantes e Alimentação S.a
            [city] => Cascais
            [racius] => http://www.racius.com/solnave-restaurantes-e-alimentacao-s-a/
            [portugalio] => http://www.portugalio.com/solnave-restaurantes-e-alimentacao-sa-2/
        )
    )
)

How can i retrieve? Thank you!

3
  • How have you tried to access it? What errors have you gotten? Commented Feb 3, 2014 at 18:18
  • Hi! I tried yes. :) i did echo $json->title and several other ways, including foreach loops and im receiving a lot of blank pages. Commented Feb 3, 2014 at 18:25
  • 2
    Does echo $yourOBJ->records->500272328->title; work? Commented Feb 3, 2014 at 18:25

3 Answers 3

2

I am assuming you are calling json_decode to decode the JSON string.

You should call it as json_decode($thestring_to_be_decoded, true). This will convert the objects to associative arrays and you will be able to access the title field.

Sign up to request clarification or add additional context in comments.

Comments

0

Your stdClass is a PHP object or a Javascript encoded object?

It seems a PHP, so to echo title element you must navigate at there:

       echo $yourobjarray->records->500272328->title;

or

       echo $yourobjarray->records->500272328['title'];

(not evident in your question)

But to echo into <script> section of a HTML page,

  <script> ... 
      var x='<?=  $yourobjarray->records->500272328['title'];?>'; 
  ...</script>

and, if a AJAX context, to JSON response you need

  echo json_encode($x);

where $x is your array or the title string, or what you want.

Comments

0

You can make use of a foreach construct to loop through your JSON decoded object array or If you want to print individual elements you can simply do like this.

echo $yourobjarray->result; //"prints" success

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.