简体   繁体   English

如何使用数组索引访问 php 中的 Json 信息

[英]How can I access Json information in php using array indexing

I want to get all prices and add them.我想获取所有价格并添加它们。 I was planning on using this to loop through the JSON but that would be another question.我打算用它来循环通过 JSON 但这将是另一个问题。 Am I approaching this the wrong way?我是以错误的方式接近这个吗? Is there a better way to get the SUM of all prices?有没有更好的方法来获得所有价格的总和?

I have found this answer which makes the same question but it does not work (duplicate: PHP: How to access array element values using array-index ).我发现这个答案提出了同样的问题,但它不起作用(重复: PHP:如何使用数组索引访问数组元素值)。 Here is my JSON string.这是我的 JSON 字符串。

{
  "data": {
    "230723": {
      "2019-11-15": {
        "price": 52,
        "min_length_of_stay": 2,
        "available": 1
      },
      "2019-11-16": {
        "price": 90,
        "min_length_of_stay": 2,
        "available": 0
      },
      "2019-11-17": {
        "price": 44,
        "min_length_of_stay": 2,
        "available": 0
      },
      "2019-11-18": {
        "price": 44,
        "min_length_of_stay": 2,
        "available": 0
      }
    }
  }
}

And here is my code:这是我的代码:

$resultJson =  file_get_contents('http://....');

$priceRange = json_decode($resultJson, true);
echo $priceRange['data']['230723']['2019-11-15']['price'];  // working

//$priceRange = array_values($priceRange);
echo $priceRange[0][0][0][0]; // not working

The first echo works and return 52. The second echo does not work with or without the commented line (which was the answer to the duplicate question).第一个回声有效并返回 52。第二个回声在有或没有注释行的情况下都不起作用(这是重复问题的答案)。

What am I missing?我错过了什么?

Well you know I hope that the data is in data and if you don't know the next key then reset helps.好吧,您知道我希望数据在data中,如果您不知道下一个键,那么reset会有所帮助。 Then just get all price keys (hopefully you know this as well) and sum them:然后只需获取所有price键(希望您也知道这一点)并将它们相加:

$result = array_sum(array_column(reset($array['data']), 'price'));

Another way is using array_values on the first two levels:另一种方法是在前两个级别上使用array_values

$result = array_sum(array_column(array_values(array_values($array)[0])[0], 'price'));

To get each price the way you were trying to do you would need:要以您尝试的方式获得每个价格,您需要:

$result = array_values(array_values(array_values(array_values($array)[0])[0])[0])[0];

Instead of changing the array from associative to numeric just loop through already existing array无需将数组从关联更改为数字,只需遍历现有数组

Like this像这样

$sum = 0;
$resultJson =  file_get_contents('http://....');

$priceRanges = json_decode($resultJson, true);
foreach ($priceRanges['data'] as $id => $priceRange) {
    if (!is_array($priceRange)) {
        continue;
    }
    foreach ($priceRange as $date => $priceInfo) {
        $sum += (isset($priceInfo['price']) ? intval($priceInfo['price']) : 0);
    }
}

The code I wrote using Krzysztof Janiszewski answer, and works.我使用Krzysztof Janiszewski回答编写的代码,并且有效。

$sum = 0;

$priceRange = json_decode($resultJson, true);

foreach($priceRange as $item) {
    foreach($item as $item2){
        foreach($item2 as $item3){
            $sum += $item3['price'];    
        }
    }
}

echo $sum;  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM