Do operations in array with values from another array

Hi I’ve got the following array:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Giuseppe
                )

            [1] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Francesco
                )

            [2] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1B
                    [Importo] => 500
                    [Condomino] => Francesco
                )

            [3] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 83.333333333333
                    [Condomino] => Alessandro
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [Descrizione] => Rata Num.2
                    [Scadenza] => 01/03/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Giuseppe
                )

            [1] => Array
                (
                    [Descrizione] => Rata Num.2
                    [Scadenza] => 01/03/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Francesco
                )

            [2] => Array
                (
                    [Descrizione] => Rata Num.2
                    [Scadenza] => 01/03/2021
                    [Interno] => 1B
                    [Importo] => 500
                    [Condomino] => Francesco
                )

            [3] => Array
                (
                    [Descrizione] => Rata Num.2
                    [Scadenza] => 01/03/2021
                    [Interno] => 1A
                    [Importo] => 83.333333333333
                    [Condomino] => Alessandro
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [Descrizione] => Rata Num.3
                    [Scadenza] => 01/05/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Giuseppe
                )

            [1] => Array
                (
                    [Descrizione] => Rata Num.3
                    [Scadenza] => 01/05/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Francesco
                )

            [2] => Array
                (
                    [Descrizione] => Rata Num.3
                    [Scadenza] => 01/05/2021
                    [Interno] => 1B
                    [Importo] => 500
                    [Condomino] => Francesco
                )

            [3] => Array
                (
                    [Descrizione] => Rata Num.3
                    [Scadenza] => 01/05/2021
                    [Interno] => 1A
                    [Importo] => 83.333333333333
                    [Condomino] => Alessandro
                )

        )

)

and the following:

Array
(
    [0] => 33.34
    [1] => 33.33
    [2] => 33.33
)

In the first array I’ve got three groups of subarrays, and I need to do some operations with the numbers from the second array. So for the first group of four subarray I need to divide the importo by 33.34 forr the second group by 33.33 and the third group by 33.33.

I’ve tried with nested forach but it gives me duplicated values. Can someone help me please? Many thanks

Where is this array data coming from in the first place? A database?

Hi @benanamen no the data doesn’t come from a database, it comes from a html form and I’m using a php library to create some sort of recurring payments.

It looks as though you will be using the same primary key for both arrays so you can use this key to identify which factor will be applied to which array group.

foreach($data as $key => $ar):	
// So $data[$key] group will use $factor[$key]
endforeach;

To get to the actual values we loop again through the sub array and can define a skey to identify this sub group.

foreach($data as $key => $ar):						
	foreach($data[$key] as $skey => $sar):

	endforeach;
endforeach;

So we want to take value $data[$key][$skey]['Importo'] and divide by $factor[$key]
For example purposes I will assign the result to a field called 'NEWImporto' , i.e. $data[$key][$skey]['NEWImporto']. All together we have this.

foreach($data as $key => $ar):						
	foreach($data[$key] as $skey => $sar):
		$data[$key][$skey]['NEWImporto'] = $data[$key][$skey]['Importo']/$factor[$key];	
	endforeach;
endforeach;

echo "<pre>";
print_r($data);
echo "</pre>";
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Giuseppe
                    [NEWImporto] => 6.2487502499499
                )

            [1] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 208.33333333333
                    [Condomino] => Francesco
                    [NEWImporto] => 6.2487502499499
                )

            [2] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1B
                    [Importo] => 500
                    [Condomino] => Francesco
                    [NEWImporto] => 14.99700059988
                )

            [3] => Array
                (
                    [Descrizione] => Rata Num.1
                    [Scadenza] => 01/01/2021
                    [Interno] => 1A
                    [Importo] => 83.333333333333
                    [Condomino] => Alessandro
                    [NEWImporto] => 2.49950009998
                )

        )
etc.
2 Likes

Hi @Drummin it works! Many thanks for the explanation as well :slight_smile:

1 Like