0

I have to do a website with PHP and actually I'm trying with it. For now I want to obtain a JSON from a URL (I've got a web service with Node.js) and show in the screen. The URL returns a JSON object like this:

[{"name":"Juan","text":"Oh my god"},{"name":"Pedro","text":"I'm here"}]

I have this code in PHP file:

<?php 
    $data = file_get_contents('http://localhost:3000/node/busca'); // Returns the JSON
    $terminos = json_decode($data);

    print_r($terminos);

    echo $terminos->name;

?>

But print_r returns:

Array ( 
     [0] => stdClass Object ( 
            [name] => Juan 
            [text] => Oh my god
         ) 
     [1] =>  stdClass Object ( 
            [name] => Pedro
            [text] => I'm here
         )
 )

The echo says

Notice: Trying to get property of non-object in C:...\index.php on line 17

What can I do? json_decode should return an object and not an array.

4
  • 2
    That's because it is an […] array at the outset. → see Commented Jan 8, 2015 at 16:52
  • And how I can delete it? Commented Jan 8, 2015 at 16:56
  • 2
    Delete what? Are you trying to handle both cases, or something? Did you expect a different data structure? Only want to use one of the entries? Which? Anything else you want to concretize? (For your question here: you can't delete it, once someone answered it.) Commented Jan 8, 2015 at 17:01
  • If you want your script to return one object, fix the script and don't have it return an array. Commented Jan 8, 2015 at 17:02

4 Answers 4

4

The JSON and the decoded PHP is an array of objects. Try:

echo $terminos[0]->name;

You have multiple array elements so:

foreach($terminos as $object) {
    echo $object->name;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Or he can loop it once to get out from the array and directly access the object via $k->name; (assuming $k assigned from foreach) since its stdClass i think?
This works (with name, sorry) but then, Can't I use print_r? I say, How can I show all the elements?
1

Your data is an encoded array of objects. So you will get an array of objects. Everything is right here.

Comments

1

Edited to OP's question to re-format the array output to:

Array ( 
     [0] => stdClass Object ( 
            [name] => Juan 
            [text] => Oh my god
         ) 
     [1] =>  stdClass Object ( 
            [name] => Pedro
            [text] => I'm here
         )
 )

Looking at it like this it is quite clear how the individual objects are wrapped and addressable:

foreach ($terminos as $idx => $obj ) {
    echo "Name $idx: " $obj->name . PHP_EOL;
    /// ... etc
}

Should output:

Name 0: Juan 
Name 1: Pedro 

Comments

0

See json_decode

mixed json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] )


$terminosFalse = json_decode($data, true);
array(2) {
  [0]=>
  array(1) {
    ["attribute"]=>
    string(1) "attr1"
  }
  [1]=>
  array(1) {
    ["attribute"]=>
    string(1) "ATTR2"
  }
}


$terminosTrue = json_decode($data, false);
array(2) {
  [0]=>
  object(stdClass)#1 (1) {
    ["attribute"]=>
        string(1) "attr1"
  }
  [1]=>
  object(stdClass)#2 (1) {
    ["attribute"]=>
    string(1) "ATTR2"
  }
}

1 Comment

That's kind of the opposite of what the OP wants.

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.