Get deltas from an array using foreach in PHP

Karuppiah RK :

I am trying to show some values using json api. It returns a date and some density value in numbers. Successfully, I have printed all return values from url. Now, I want to find the difference between one density value to previous day density value.

I know how to calculate the difference in normal static numbers. But, I am not sure how do I achieve this inside a foreach loop.

PHP Code:

$json_url = "https://www.example.com/api";

$json = file_get_contents($json_url);
$obj  = json_decode($json,true);
$data = $obj["data"];

foreach(array_slice($data, 0, 10) as $k) {

    echo $k["date"]." - ".$k["density"]."<br/><br/>";

}

Current Output:

14-02-2020 - 110

13-02-2020 - 114

12-02-2020 - 112

11-02-2020 - 110

10-02-2020 - 105

07-02-2020 - 125

06-02-2020 - 122

05-02-2020 - 118

04-02-2020 - 119

03-02-2020 - 119

Expected Output:

14-02-2020 - 110 - Diff is -4 from Previous Date

13-02-2020 - 114 - Diff is +2 from Previous Date

12-02-2020 - 112 - Diff is +2 from Previous Date

11-02-2020 - 110 - Diff is +5 from Previous Date

10-02-2020 - 105 - Diff is -20 from Previous Date

07-02-2020 - 125 - Diff is +3 from Previous Date

06-02-2020 - 122 - Diff is +4 from Previous Date

05-02-2020 - 118 - Diff is -1 from Previous Date

04-02-2020 - 119 - No Difference - Same

03-02-2020 - 119 - No Difference - Same
Sherif :

To achieve the desired effect you will need to iterate the array two elements at a time. This can't easily be done with foreach outright. So instead I will implement a generator that returns pairs of values from the array at a time.

function getPairs(Array $array) {
    reset($array);
    do {
        $a1  = current($array);
        $key = key($array);
        $a2  = next($array);
        yield [$a1, $a2];
    } while($key = key($array) !== null);
}

You can then use this generator implementation to traverse the array, attaining the desired effect.

$array = [
    ["date" => "14-02-2020", "density" => 110],
    ["date" => "13-02-2020", "density" => 114],
    ["date" => "12-02-2020", "density" => 112],
    ["date" => "11-02-2020", "density" => 110],
    ["date" => "10-02-2020", "density" => 105],
    ["date" => "07-02-2020", "density" => 125],
    ["date" => "12-02-2020", "density" => 112],
    ["date" => "06-02-2020", "density" => 122],
    ["date" => "05-02-2020", "density" => 118],
    ["date" => "04-02-2020", "density" => 119],
    ["date" => "03-02-2020", "density" => 119],
];

foreach(getPairs($array) as [$a, $b]) {
    // We must check if $b is empty to account for end of the array
    if ($b) {
        $diff = $a["density"] - $b["density"];
    } else { // Otherwise there are no more elements to diff against
        $diff = 0;
    }
    if ($diff > 0) {
        $diff = "+$diff";
    }
    echo "{$a['date']} - {$a['density']} - Diff is $diff from Previous Date<br><br>";
}

This should give you the desired output.

14-02-2020 - 110 - Diff is -4 from Previous Date

13-02-2020 - 114 - Diff is +2 from Previous Date

12-02-2020 - 112 - Diff is +2 from Previous Date

11-02-2020 - 110 - Diff is +5 from Previous Date

10-02-2020 - 105 - Diff is -20 from Previous Date

07-02-2020 - 125 - Diff is +13 from Previous Date

12-02-2020 - 112 - Diff is -10 from Previous Date

06-02-2020 - 122 - Diff is +4 from Previous Date

05-02-2020 - 118 - Diff is -1 from Previous Date

04-02-2020 - 119 - Diff is 0 from Previous Date

03-02-2020 - 119 - Diff is 0 from Previous Date

N.B.

Technically speaking you aren't actually getting the difference between the current date and the previous date, here. Rather, you are getting the difference between the current date and the next date. If you were getting the deltas between previous dates you wouldn't need the information from the next date at all. Instead you'd just use a temporary variable to store the last density value and diff against that with each iteration. However, I've tailored my answer to suit your expected result.

Otherwise the expected output would actually be:

14-02-2020 - 110 - Diff is +110 from Previous Date

13-02-2020 - 114 - Diff is +4 from Previous Date

12-02-2020 - 112 - Diff is -2 from Previous Date

11-02-2020 - 110 - Diff is -2 from Previous Date

10-02-2020 - 105 - Diff is -5 from Previous Date

07-02-2020 - 125 - Diff is +20 from Previous Date

12-02-2020 - 112 - Diff is -13 from Previous Date

06-02-2020 - 122 - Diff is +10 from Previous Date

05-02-2020 - 118 - Diff is -4 from Previous Date

04-02-2020 - 119 - Diff is +1 from Previous Date

03-02-2020 - 119 - Diff is 0 from Previous Date

And to achieve that you don't need the generator implementation I used at all. You can simply do this with a regular foreach loop like so:

$lastDiff = 0; // Initialize the diff to 0

foreach($array as $a) {
    $date = $a['date'];
    $density = $a['density'];
    $diff = $a["density"] - $lastDiff; // diff against the last known value
    if ($diff > 0) {
        $diff = "+$diff";
    }
    echo "$date - $density - Diff is $diff from Previous Date<br><br>";
    $lastDiff = $a["density"]; // store the last known density here for the next iteration
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=9835&siteId=1