-4

I have JSON data:

[
  {
    "title": "red",
  },
  {
    "title": "blue",
  },
  {
    "title": "yellow",
  },

]

I want to get only first data, red. I try with this

    ...

    $json_output=curl_exec($ch);
    $mydata = json_decode($json_output);

  $result = $mydata->title;

    echo $result[1];

and

$result = $mydata->title[1];

but dont work.

How i can get only first "title" data from this json?

ahmed_max
  • 5
  • 7

3 Answers3

1
$json_output=curl_exec($ch);
$mydata = json_decode($json_output);

$result = $mydata[0]->title;
echo $result;
Gian Tomakin
  • 193
  • 1
  • 5
1

First of all your JSON is not valid. You can use this validator to check if your JSON is valid. It should look like the following:

[
  {
    "title": "red"
  },
  {
    "title": "blue"
  },
  {
    "title": "yellow"
  }

]

There are two ways of accessing the JSON object:

  1. Array of objects:

    $mydata = json_decode($json_output);
    $title = $mydata[0]->title; // red
    
  2. Associative array:

    $mydata = json_decode($json_output, true);
    $title = $mydata[0]['title']; // red
    

See json_decode() for more information.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
rbr94
  • 2,227
  • 3
  • 23
  • 39
1

According to PHP's manual, json_decode returns the value encoded in JSON in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the JSON cannot be decoded or if the encoded data is deeper than the recursion limit.

<?php
    $json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]';
    $mydata = json_decode($json_output);
    var_dump($mydata);
    /* Output:
    array(3) {
      [0]=>
      object(stdClass)#1 (1) {
        ["title"]=>
        string(3) "red"
      }
      [1]=>
      object(stdClass)#2 (1) {
        ["title"]=>
        string(4) "blue"
      }
      [2]=>
      object(stdClass)#3 (1) {
        ["title"]=>
        string(6) "yellow"
      }
    }
    */
    echo $mydata[0]->title;
    // Output: red
?>

When 2nd parameter is TRUE, returned objects will be converted into associative arrays.

<?php
    $json_output = '[{ "title": "red" }, { "title": "blue" }, { "title": "yellow" }]';
    $mydata = json_decode($json_output, TRUE);
    var_dump($mydata);
    /* Ouput:
    array(3) {
      [0]=>
      array(1) {
        ["title"]=>
        string(3) "red"
      }
      [1]=>
      array(1) {
        ["title"]=>
        string(4) "blue"
      }
      [2]=>
      array(1) {
        ["title"]=>
        string(6) "yellow"
      }
    }
    */
    echo $mydata[0]['title'];
    // Output: red
?>

On a side note, accessing elements within an object that contain any character not allowed under PHP's naming convention can be done by wrapping the index with a curly bracket.

<?php
    $json_output = '[{ "h1-title": "red" }, { "h1-title": "blue" }, { "h1-title": "yellow" }]';
    $mydata = json_decode($json_output);
    var_dump($mydata);
    /* Output:
    array(3) {
      [0]=>
      object(stdClass)#1 (1) {
        ["h1-title"]=>
        string(3) "red"
      }
      [1]=>
      object(stdClass)#2 (1) {
        ["h1-title"]=>
        string(4) "blue"
      }
      [2]=>
      object(stdClass)#3 (1) {
        ["h1-title"]=>
        string(6) "yellow"
      }
    }
    */
    echo $mydata[0]->{'h1-title'};
    // Output: red
?>
Daerik
  • 4,167
  • 2
  • 20
  • 33
  • I personally wouldn't put to much time in an *about to close* answer, especially when its a lower ranked user. They seem to forget they asked this question, don't even mark or+1 it, but hey all `+1` from me for effort. – Xorifelse Nov 23 '16 at 21:37