0

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. 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). Here is my JSON string.

{
  "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).

What am I missing?

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Gluxable
  • 75
  • 9
  • You would have to apply `array_values()` at each level of the array. – Nigel Ren Nov 18 '19 at 14:52
  • Would I have to use a loop for that? Could you elaborate a bit more? Thanks. – Gluxable Nov 18 '19 at 14:57
  • `echo $priceRange[0][0][0][0]; // not working` – why _should_ it work, what do you need this for? _If_ you actually applied array_values recursively, then you would lose the info currently contained in those keys completely - so you don’t need `230723` and the actual date values on the next level at all …? – 04FS Nov 18 '19 at 15:02
  • 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. Am I approaching this the wrong way? Is there a better way to get the SUM of all prices? – Gluxable Nov 18 '19 at 15:14
  • Really!?!?..... – AbraCadaver Dec 07 '19 at 00:25

3 Answers3

1

Well you know I hope that the data is in data and if you don't know the next key then reset helps. Then just get all price keys (hopefully you know this as well) and sum them:

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

Another way is using array_values on the first two levels:

$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];
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1

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);
    }
}
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
  • I could not get your code to work but I used it to write the following code (*see my own answer). It returns the sum but how bad is it concerning time complexity? – Gluxable Nov 19 '19 at 08:45
  • @Gluxable I was missing one closing parentheses `)` I edited my answer, should be fine now – Krzysztof Janiszewski Nov 19 '19 at 11:54
0

The code I wrote using Krzysztof Janiszewski answer, and works.

$sum = 0;

$priceRange = json_decode($resultJson, true);

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

echo $sum;  
Gluxable
  • 75
  • 9