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?

5
  • You would have to apply array_values() at each level of the array. Commented Nov 18, 2019 at 14:52
  • Would I have to use a loop for that? Could you elaborate a bit more? Thanks. Commented Nov 18, 2019 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 …? Commented Nov 18, 2019 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? Commented Nov 18, 2019 at 15:14
  • Really!?!?..... Commented Dec 7, 2019 at 0:25

3 Answers 3

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);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 I was missing one closing parentheses ) I edited my answer, should be fine now
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];

Comments

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;  

1 Comment

This will work, but there is no fail safe. If something changes (or some elements lacks a value) your code will crush.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.