0

I'm learning to work with an array, I have an array in which I have a starting date and a payment amount in every array which I try to merge the same date array in one array and add their payment amount in one array
this exiting array

array:4 [
  0 => array[
    "payment_amount" => 100
    "starting_date" => "2021-06-16"
  ]
  1 => array[
    "payment_amount" => 200
    "starting_date" => "2021-06-16"
  ]
  2 => array[
    "payment_amount" => 300
    "starting_date" => "2021-06-16"
  ]
  3 => array[
    "payment_amount" => -200
    "starting_date" => "2021-06-17"
  ]
]

Expected Result

array:2 [
      0 => array[
        "payment_amount" => 600
        "starting_date" => "2021-06-16"
      ]
      1 => array[
        "payment_amount" => -200
        "starting_date" => "2021-06-17"
      ]
    ]

This is my script code which I try to merge the array

$.ajax({
        url: '/test/data',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            data.sort(function(a,b){
                return new Date(a.starting_date) - new Date(b.starting_date);
              });
            testarray(data) 
        },
        error: function () {
            alert("Failed! Please try again.");
        }
    });

My php code

 $sortarray = [];
        $previewsdate = 0;
        $newsdate = 0;
        for ($i=0;$i <count($result);$i++) {
            $previewsdate = $result[$i]['starting_date'];
            echo "previewsdate ".$newsdate.'<br>';
            if ($result['0']['starting_date'] === $previewsdate) {
                $newsdate = $previewsdate;
                echo "first date".$newsdate.'<br>';
                array_push($sortarray, $result[$i]);
           }
            else if ($newsdate === $previewsdate) {
                $newone = array_merge( $result[$i], $result[$i-1]);
                $newsdate = $previewsdate;
               print_r($newsdate);
                echo "next datekjkjkj ".$newsdate.'<br>';
                array_push($sortarray, $newone);
           }
           else if ($newsdate !== $previewsdate) {
            $newsdate = $previewsdate;
            echo "next date ".$newsdate.'<br>';
            array_push($sortarray, $result[$i]);
            }
        }
5
  • 1
    except you're not trying to merge the array at all. You're sorting it. Have you made an attempt at merging? Pls show it. Commented Jun 16, 2021 at 10:24
  • check I update the merge code in question but its not working @Kinglish Commented Jun 16, 2021 at 10:41
  • Do you need this to sort in php? You don't have that tag in your question Commented Jun 16, 2021 at 10:53
  • probable duplicate Commented Jun 16, 2021 at 11:22
  • Does this answer your question? JSON - Sum values by date using php Commented Jun 16, 2021 at 12:40

1 Answer 1

0

please check this

$a = [
  0 => [
    "payment_amount" => 100,
    "starting_date" => "2021-06-16"
  ],
  1 => [
    "payment_amount" => 200,
    "starting_date" => "2021-06-16"
  ],
  2 => [
    "payment_amount" => 300,
    "starting_date" => "2021-06-16"
  ],
  3 => [
    "payment_amount" => -200,
    "starting_date" => "2021-06-17"
  ],
];
$p = array();
foreach($a as $row){
if(array_key_exists($row['starting_date'], $p)){
$p[$row['starting_date']] = $p[$row['starting_date']] + $row['payment_amount'];
}else{
$p[$row['starting_date']] = $row['payment_amount'];
}
}

$finalArr = array();
foreach($p as $dateKey => $totalAmount){
$temp = array();
$temp['payment_amount'] = $totalAmount;
$temp['starting_date'] = $dateKey;
array_push($finalArr, $temp);
}

And you will get final result in $finalArr

Sign up to request clarification or add additional context in comments.

1 Comment

probable duplicate

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.