【发布时间】:2016-05-16 10:43:44
【问题描述】:
我有一个类似于下面给出的 JSON 字符串,它存储在 $json 中。我想在不使用循环的情况下访问第一个 category_id。如何访问它?
$json = '{"outfits": [{"1": [{"category_id": "women_jeans", "product_id": 464540467}, {"category_id": "women_tops", "product_id": 487351815}, {"category_id": "women_coats", "product_id": 493322686}, {"category_id": "women_bags", "product_id": 483902882}, {"category_id": "women_shoes", "product_id": 492772225}]}]}';
$outfits = json_decode($json);
【问题讨论】:
-
您有多个
category_id可用。您究竟想访问哪一个? -
@Nikola : 第一个具有 product_id : 464540467
-
$outfits = json_decode($json, true); var_dump($outfits['outfits'][0][1][0]["category_id"]);
-
@RakeshSojitra,你可以在没有
true元素的json_decode中做到这一点。 -
是的,你可以做到。
true将为您提供没有对象数组的简单数组。