数组显示但子数组显示“非法字符串偏移”错误-PHP

时间:2019-11-18 00:09:08

标签: php arrays

这里是

的示例
$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;

这会打印[{“ id”:“ 8080”,“ content”:“ foo”,“ member”:[123,456],“ interval”:7}]

但是当我尝试获取某个元素的特定值(例如“内容”)时,它显示了一个非法的字符串偏移错误

echo $final["content"];

我应该怎么做才能显示“内容”的价值? (在这种情况下为“ foo”)

2 个答案:

答案 0 :(得分:1)

echo $final["content"];更改为echo $results[0]["content"];

答案 1 :(得分:0)

实际上,您不需要let newArray = varNames.filter({!reservedWords.contains($0)})

直接输出到

$final = json_encode($results);