skip to Main Content

I created an array like this

    Array
(
    [0] => Array
        (
            [amount] => 13.31000000
            [i] => 1
            [created_at] => 2022-10-10 23:21:47
        )

    [1] => Array
        (
            [amount] => 19.00000000
            [i] => 2
            [created_at] => 2022-10-10 23:21:38
        )

    [2] => Array
        (
            [amount] => 17.32000000
            [i] => 2
            [created_at] => 2022-10-09 23:21:47
        )

    [3] => Array
        (
            [amount] => 19.15000000
            [i] => 2
            [created_at] => 2022-10-08 23:21:47
        )

    [4] => Array
        (
            [amount] => 14.68000000
            [i] => 2
            [created_at] => 2022-10-07 23:21:47
        )

    [5] => Array
        (
            [amount] => 18.31000000
            [i] => 2
            [created_at] => 2022-10-06 23:21:47
        )

    [6] => Array
        (
            [amount] => 19.48000000
            [i] => 2
            [created_at] => 2022-10-05 23:21:47
        )

    [7] => Array
        (
            [amount] => 11.20000000
            [i] => 1
            [created_at] => 2022-10-04 23:21:47
        )

    [8] => Array
        (
            [amount] => 12.91000000
            [i] => 2
            [created_at] => 2022-10-03 23:21:47
        )

    [9] => Array
        (
            [amount] => 11.55000000
            [i] => 2
            [created_at] => 2022-10-02 23:21:47
        )

    [10] => Array
        (
            [amount] => 13.64000000
            [i] => 1
            [created_at] => 2022-10-09 23:21:38
        )

    [11] => Array
        (
            [amount] => 18.44000000
            [i] => 1
            [created_at] => 2022-10-08 23:21:38
        )

    [12] => Array
        (
            [amount] => 11.56000000
            [i] => 2
            [created_at] => 2022-10-07 23:21:38
        )

    [13] => Array
        (
            [amount] => 12.13000000
            [i] => 1
            [created_at] => 2022-10-06 23:21:38
        )

    [14] => Array
        (
            [amount] => 13.39000000
            [i] => 1
            [created_at] => 2022-10-05 23:21:38
        )

    [15] => Array
        (
            [amount] => 11.36000000
            [i] => 2
            [created_at] => 2022-10-04 23:21:38
        )

    [16] => Array
        (
            [amount] => 11.82000000
            [i] => 1
            [created_at] => 2022-10-03 23:21:38
        )

    [17] => Array
        (
            [amount] => 14.03000000
            [i] => 1
            [created_at] => 2022-10-02 23:21:38
        )

)

I want to merge same date and do the increase or decrease based on [i] value

I already created this foreach

foreach ($result as $k => $y) {
  $info = "decrease";
  if($y['i']==1){
    $info = "increase";
  }
  $d = date('mdy', strtotime($y['created_at']));
  $newDates[$d] = $d;
  if(isset($res[$d]["increase"])){
    $res[$d]["increase"] += $y['amount'];
  }else{
    $res[$d]["increase"] = 0;
  }
  if(isset($res[$d]["decrease"])){
    $res[$d]["decrease"] += $y['amount'];
  }else{
    $res[$d]["decrease"] = 0;
  }
}

What I get for the result is like this

Array
(
    [101022] => Array
        (
            [increase] => 19
            [decrease] => 19
        )

    [100922] => Array
        (
            [increase] => 13.64
            [decrease] => 13.64
        )

    [100822] => Array
        (
            [increase] => 18.44
            [decrease] => 18.44
        )

    [100722] => Array
        (
            [increase] => 11.56
            [decrease] => 11.56
        )

    [100622] => Array
        (
            [increase] => 12.13
            [decrease] => 12.13
        )

    [100522] => Array
        (
            [increase] => 13.39
            [decrease] => 13.39
        )

    [100422] => Array
        (
            [increase] => 11.36
            [decrease] => 11.36
        )

    [100322] => Array
        (
            [increase] => 11.82
            [decrease] => 11.82
        )

    [100222] => Array
        (
            [increase] => 14.03
            [decrease] => 14.03
        )

)

But it should be like this for the result

Array
(
[101022] => Array
(
[increase] => 13.31
[decrease] => 19
)

    [100922] => Array
        (
            [increase] => 13.64
            [decrease] => 17.32
        )

    [100822] => Array
        (
            [increase] => 18.44
            [decrease] => 19.15
        )

    [100722] => Array
        (
            [increase] => 0
            [decrease] => 26.24
        )

    [100622] => Array
        (
            [increase] => 12.13
            [decrease] => 18.31
        )

    [100522] => Array
        (
            [increase] => 13.39
            [decrease] => 19.48
        )

    [100422] => Array
        (
            [increase] => 11.20
            [decrease] => 11.36
        )

    [100322] => Array
        (
            [increase] => 11.82
            [decrease] => 12.91
        )

    [100222] => Array
        (
            [increase] => 14.03
            [decrease] => 11.55
        )

)

For the data in date 100722 the increase should be 0 because the data give all decrease
[i] = 1 // increase
[i] = 2 // decrease

thanks for helping me

2

Answers


  1. When creating the array, you could set the values to zero. This also ensures the increase and decrease key stay in order.

    $array = [
        ['amount' => 13.31000000, 'i' => 1, 'created_at' => '2022-10-10 23:21:47',],
        ['amount' => 19.00000000, 'i' => 2, 'created_at' => '2022-10-10 23:21:38',],
        ['amount' => 17.32000000, 'i' => 2, 'created_at' => '2022-10-09 23:21:47',],
        ['amount' => 19.15000000, 'i' => 2, 'created_at' => '2022-10-08 23:21:47',],
        ['amount' => 14.68000000, 'i' => 2, 'created_at' => '2022-10-07 23:21:47',],
        ['amount' => 18.31000000, 'i' => 2, 'created_at' => '2022-10-06 23:21:47',],
        ['amount' => 19.48000000, 'i' => 2, 'created_at' => '2022-10-05 23:21:47',],
        ['amount' => 11.20000000, 'i' => 1, 'created_at' => '2022-10-04 23:21:47',],
        ['amount' => 12.91000000, 'i' => 2, 'created_at' => '2022-10-03 23:21:47',],
        ['amount' => 11.55000000, 'i' => 2, 'created_at' => '2022-10-02 23:21:47',],
        ['amount' => 13.64000000, 'i' => 1, 'created_at' => '2022-10-09 23:21:38',],
        ['amount' => 18.44000000, 'i' => 1, 'created_at' => '2022-10-08 23:21:38',],
        ['amount' => 11.56000000, 'i' => 2, 'created_at' => '2022-10-07 23:21:38',],
        ['amount' => 12.13000000, 'i' => 1, 'created_at' => '2022-10-06 23:21:38',],
        ['amount' => 13.39000000, 'i' => 1, 'created_at' => '2022-10-05 23:21:38',],
        ['amount' => 11.36000000, 'i' => 2, 'created_at' => '2022-10-04 23:21:38',],
        ['amount' => 11.82000000, 'i' => 1, 'created_at' => '2022-10-03 23:21:38',],
        ['amount' => 14.03000000, 'i' => 1, 'created_at' => '2022-10-02 23:21:38',],
    ];
    
    $result = [];
    foreach ($array as $item) {
        $key     = date('mdy', strtotime($item['created_at']));
        $operand = $item['i'] === 1 ? 'increase' : 'decrease';
        if (!isset($result[$key])) $result[$key] = ['increase' => 0, 'decrease' => 0,];
        $result[$key][$operand] += $item['amount'];
    }
    
    print_r($result);
    

    Output

    Array
    (
        [101022] => Array
            (
                [increase] => 13.31
                [decrease] => 19
            )
    
        [100922] => Array
            (
                [increase] => 13.64
                [decrease] => 17.32
            )
    
        [100822] => Array
            (
                [increase] => 18.44
                [decrease] => 19.15
            )
    
        [100722] => Array
            (
                [increase] => 0
                [decrease] => 26.24
            )
    
        [100622] => Array
            (
                [increase] => 12.13
                [decrease] => 18.31
            )
    
        [100522] => Array
            (
                [increase] => 13.39
                [decrease] => 19.48
            )
    
        [100422] => Array
            (
                [increase] => 11.2
                [decrease] => 11.36
            )
    
        [100322] => Array
            (
                [increase] => 11.82
                [decrease] => 12.91
            )
    
        [100222] => Array
            (
                [increase] => 14.03
                [decrease] => 11.55
            )
    
    )
    
    Login or Signup to reply.
  2. My recommendation is functionally identical to Markus’s answer, but makes some different style and function call choices.

    Create a lookup array that maps i integers to plain English words; because this map will never change during the script execution, make it a constant.

    Then create an array of default values to be used by every newly encountered day group.

    Then loop over your input array’s rows, isolate the date substring from the datetime string — this will be your grouping key.

    If the group is being encountered for the first time, the null coalescing assignment operator will apply the default elements, otherwise this line will be ignored.

    Finally, add the current amount to the respective element in the group.

    Code: (Demo)

    const LOOKUP = [1 => 'increase', 2 => 'decrease'];
    $defaults = array_fill_keys(LOOKUP, 0);
    
    $result = [];
    foreach ($array as $row) {
        $date = substr($row['created_at'], 0, 10);
        $result[$date] ??= $defaults;
        $result[$date][LOOKUP[$row['i']]] += $row['amount'];
    }
    
    print_r($result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search