我有这个
{
"items":[
{
"id":463282624,
"original_id":463282624,
"defindex":10175,
"level":1,
"quality":4,
"inventory":2147483980,
"quantity":1,
"attributes":[
{
"defindex":187,
"value":1106771968,
"float_value":31
}
]
},
{
"id":465686099,
"original_id":465686099,
"defindex":10175,
"level":1,
"quality":4,
"inventory":2147483979,
"quantity":1,
"attributes":[
{
"defindex":187,
"value":1106771968,
"float_value":31
}
]
}
]
}
如何使用[' defindex'] = 10175
取出商品的[' id']请帮忙!
答案 0 :(得分:2)
PHP没有提供任何通过内容检索元素的方法,所以你必须写一个循环:
foreach ($object['items'] as $item) {
if ($item['defindex'] == 10175) {
$id = $item['id'];
break;
}
}
如果您需要重复执行此操作,则应将数据转换为使用defindex
作为键的关联数组,然后您可以轻松访问它们。
$items_by_defindex = array();
foreach ($object['items'] as $item) {
$items_by_defindex[$item['defindex']] = $item;
}
$id = $items_by_defindex[10175];