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)

3
  • 3
    $final is a JSON string. If you want the value of content then echo $results[0]['content']; Commented Nov 18, 2019 at 0:12
  • echo $final["content"]; can not work because $final is string. Commented Nov 18, 2019 at 0:12
  • Does this answer your question? How do I extract data from JSON with PHP? Commented Nov 18, 2019 at 3:03

2 Answers 2

1

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

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

Comments

0

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

directly output to

echo $results[0]["content"];

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.