嗨,大家好,我想知道如何基於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.
非常感謝