访问嵌套的JSON键而不进行循环


Accessing a nested JSON key without looping

我有一个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);

请尝试以下代码。

$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,true);
echo "Category 1: ".$outfits["outfits"][0][1][0]['category_id'];
echo "Category 2: ".$outfits["outfits"][0][1][1]['category_id'];

输出

women_jeans
women_tops

您也可以使用下面的函数来查找特定的值。

$array = $outfits["outfits"][0][1];
$key = "product_id";
$val = "464540467";
function whatever($array, $key, $val) {
    foreach ($array as $item)
        if (isset($item[$key]) && $item[$key] == $val)
            echo $item['category_id'];
    return false;
}
whatever($array,$key,$val);

输出

women_jeans

如果您想在没有循环和true的情况下获得category_id,请使用以下内容。

$array = $outfits->outfits[0]->{1};
$category1 = $array[0]->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,true);

Eg。

<?php
$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, true);

$arr = $outfits["outfits"][0][1];

直接获取价值:

echo $arr[0]['category_id'].", ".$arr[1]['category_id'];
function walk($v, $k) {
    echo "$k : ".$v['category_id']."<br/>";
}

如果您不想要自己的循环:,可以使用array_walk

array_walk($arr, "walk");
?>
$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}]}]}';
//convert json to associative array.  
$json_array = json_decode($json,true);
// Return the values from a single column in the input array 
$newArray= array_column($json_array["outfits"][0][1],"category_id");

输出

Array
(
    [0] => women_jeans
    [1] => women_tops
    [2] => women_coats
    [3] => women_bags
    [4] => women_shoes
)

注意:array_column仅适用于5.5+

OP的要求我想在没有循环的情况下访问它这不是正确的原因是你有多个category_id,所以问题是你需要哪一个。所以你需要循环。

通过使用json_decode,您将拥有一个对象数组,因此您需要执行这些操作。让$json是你的字符串。通过在foreach循环中使用$outfits->outfits[0]->{1},只需获得结果数组的最后一个维度,因此访问category_id很容易。

$outfits = json_decode($json);
foreach($outfits->outfits[0]->{1} as $val){
    echo $val->category_id."<br/>";
}

结果:

women_jeans
women_tops
women_coats
women_bags
women_shoes