1

I need to echo the value of "summary"

    {  
   "expand":"names",
   "startAt":0,
   "maxResults":50,
   "total":1,
   "issues":[  
      {  
         "expand":"example",
         "id":"129018",
         "self":"https://example.com",
         "key":"914",
         "fields":{  
            "summary":"Hello there"
         }
      }
   ]
}

The below code does not work:

$array_result = json_decode($run_curl, true);
    $title = $array_result['issues']['fields']['summary];

What am I doing wrong here? I am pretty sure it's smth simple and obvious.

bianchi
  • 500
  • 2
  • 12

3 Answers3

2

issues is an array, pass it an index like this:

$title = $array_result['issues'][0]['fields']['summary'];

Note the [0].

avip
  • 1,445
  • 13
  • 14
0

I miss something called true at json_decode($json, true);.

If it is true then use: echo $array_result[issues][0][fields][summary];

Online link, This is the online link where you can check it.

$json = '{  
   "expand":"names",
   "startAt":0,
   "maxResults":50,
   "total":1,
   "issues":[  
      {  
         "expand":"example",
         "id":"129018",
         "self":"https://example.com",
         "key":"914",
         "fields":{  
            "summary":"Hello there"
         }
      }
   ]
}';

JSON Decode

When you start using json_decode, this function makes some array as object, so to access array you need to use the -> sign.

$array_result = json_decode($json);
echo '<pre>';
print_r($array_result);
echo '</pre>';
echo $array_result->issues[0]->fields->summary;

Output

Decoded array:

stdClass Object
(
    [expand] => names
    [startAt] => 0
    [maxResults] => 50
    [total] => 1
    [issues] => Array
        (
            [0] => stdClass Object
                (
                    [expand] => example
                    [id] => 129018
                    [self] => https://example.com
                    [key] => 914
                    [fields] => stdClass Object
                        (
                            [summary] => Hello there
                        )

                )

        )

)

Hello there

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
0

issues is a list ,so you need to add key=0 to get the first element:

$title = $array_result['issues'][0]['fields']['summary'];
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
suibber
  • 267
  • 1
  • 6