Subtract number from array values

Hi I’ve got the following array:

Array
(
    [20] => Array
        (
            [condomino] => 20
            [anagrafica] => Alessandro Rossi
            [immobile] => Immobile 1A
            [paid] => 150.00
            [01/01/2021] => Array
                (
                    [to_pay] => 83.35
                    [scaduta] => 1
                )

            [01/05/2021] => Array
                (
                    [to_pay] => 83.33
                    [scaduta] => 1
                )

            [25/12/2021] => Array
                (
                    [to_pay] => 83.33
                    [scaduta] => 1
                )

        )

)

I need to achieve the following array:

Array
(
    [20] => Array
        (
            [condomino] => 20
            [anagrafica] => Alessandro Rossi
            [immobile] => Immobile 1A
            [paid] => 150.00
            [01/01/2021] => Array
                (
                    [to_pay] => 83.35
                    [scaduta] => 1
                    [left_to_pay] => 0
                )

            [01/05/2021] => Array
                (
                    [to_pay] => 83.33
                    [scaduta] => 1
                    [left_to_pay] =>16.68
                )

            [25/12/2021] => Array
                (
                    [to_pay] => 83.33
                    [scaduta] => 1
                    [left_to_pay] =>83.33
                )

        )

)

So basibally I need to subtract each to_pay value from paid value and then use what is left from each operation to carry on into the next subarray. Can anyboby please give me some help or point me into the right direction? Many thanks

Where is this data coming from and how much control do you have over the format of the array, because having the dates as an un-categorized index at the same level with other indexes will make looping over the data slow and as hard as possible, requiring that all the index values be parsed to find which ones are dates/not the other indexes? Also, will the dates be in correct order in the array, since that date format is not directly sortable?

Short-version: store the date arrays together under a main index, such as [dates], and use a YYYY-MM-DD format internally for date values.

1 Like

Hi @mabismad I totally agree with you, definitely I need to sort the array better and also optimize my database query. Definitely there is a lot of room for speed optimization. For now I think I’ve achieved what I need in this way:

         foreach ($newRateArray as $a_key => &$a_value) {

             $pagato = $a_value['paid'];

             foreach ($a_value as $b_key => &$b_value) {

               if(is_iterable($b_value)){

                 if($b_value['to_pay'] > $pagato) {

                   $b_value['left_to_pay'] = $b_value['to_pay'] - $pagato;
                   $pagato = 0;
                 }else{

                   $pagato -= $b_value['to_pay'];
                   $b_value['left_to_pay'] = 0;

                 }

               }

             }

         }

Those hardcoded zeroes seem to imply that there is an assumption here that people will never pay too much. Please double check that assumption. If it ever doesn’t hold debugging is going to be hard.

Sure would be nice to see the database table(s) structure this array is queried from.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.