访问嵌套的JSON密钥而不循环

时间:2016-05-16 10:43:45

标签: php arrays json object

我有一个类似下面给出的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);

5 个答案:

答案 0 :(得分:5)

尝试以下代码。

$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

如果你想获得没有循环的category_id和true,请使用下面的内容。

$array = $outfits->outfits[0]->{1};
$category1 = $array[0]->category_id;

答案 1 :(得分:2)

 $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);

答案 2 :(得分:0)

例如

<?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");
?>

答案 3 :(得分:0)

$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 +

答案 4 :(得分:0)

  

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
相关问题