嗨,大家好,我想知道如何基于ID返回索引以下。返回索引从JSON返回数组
$friends = '{
"data": [
{
"name": "Paul",
"id": "12000"
},
{
"name": "Bonnie",
"id": "120310"
},
{
"name": "Melissa",
"id": "120944"
},
{
"name": "Simon",
"id": "125930"
},
{
"name": "Anthony",
"id": "120605"
},
{
"name": "David",
"id": "120733"
}
]
}';
$obj = json_decode($friends);
例如,我想打印基于ID的名称。我已经去使用array_search,但它不喜欢数组的结构。我将在sql循环中使用它,并从查询中传递ID以从数组中返回名称字符串。
print $obj->{'data'}[0]->{'name'};
//where 0 is returned index based on a defined ID.
非常感谢