【问题标题】:Accessing a nested JSON key without looping在不循环的情况下访问嵌套的 JSON 键
【发布时间】: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 将为您提供没有对象数组的简单数组。

标签: php arrays json object


【解决方案1】:

试试下面的代码。

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

【讨论】:

  • 使用上述函数获取第一个具有 product_id : 464540467.@Rakesh Sojitra
  • 是的,我只是忘了写 json_decode($json,true);真的。这就是问题所在。
  • 您可以在没有true 的情况下执行此操作。 Check my answer
  • 是的,这也可以实现,而且他们需要它而无需他提到的 foreach 循环。
  • 检查编辑的代码是否没有循环和true你可以获得第一个类别值。 @RakeshSojitra
【解决方案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);

【讨论】:

  • 是的,我只是在 json_decode($json,true);无论如何,谢谢。
【解决方案3】:

例如。

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

【讨论】:

    【解决方案4】:
    $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+

    【讨论】:

      【解决方案5】:

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

      通过使用json_decode,您将拥有一个对象数组,因此您需要这样做。让$json 是你的字符串。通过在 foreach 循环中使用$outfits-&gt;outfits[0]-&gt;{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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-02
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多