1

Here's a sample of

$arr = json_decode('{"people":[
{
  "id": "8080",
  "content": "foo",
  "member": [123, 456],
  "interval": 7
},
{ 
  "id": "8097",
  "content": "bar",
  "member": [1234, 4567],
  "interval": 7
}


]}', true);

$searchId = 123;
$results = array_filter($arr['people'], function($people) use ($searchId) {
    return in_array($searchId, $people['member']);
});

$final = json_encode($results);

echo $final;

This prints [{"id":"8080","content":"foo","member":[123,456],"interval":7}]

But when I try to get the specific value of an element (e.g. "content"), it shows me an illegal string offset error

echo $final["content"];

What should I be doing instead to show the value of "content"? (which would be "foo" in this case)

jimothy
  • 13
  • 2
  • 3
    `$final` is a JSON string. If you want the value of `content` then `echo $results[0]['content'];` – Nick Nov 18 '19 at 00:12
  • `echo $final["content"];` can not work because $final is string. – daremachine Nov 18 '19 at 00:12
  • Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – miken32 Nov 18 '19 at 03:03

2 Answers2

1

Change echo $final["content"]; to echo $results[0]["content"];

LF00
  • 27,015
  • 29
  • 156
  • 295
0

Actually , you don't need to $final = json_encode($results);

directly output to

echo $results[0]["content"];
Zar Ni Ko Ko
  • 352
  • 2
  • 7