Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i sum array elements as an array in laravel

i have two arrays and i want to make calculations with the elements in these arrays.

$result1=DB::where('elements',$elements)->get();

$result2=DB::where('otherElements',$otherElements)->get();

for example; $result1 = [5,7,9] and $result2 = [0,5,9]

$result3 must be (for multiply) [0,35,81] or (for sum) [5,12,18]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i tried to do with for loop but it does not solve my problem in blade file.

so i want to do it in controller then send the result to the blade file.

>Solution :

You can use array_map same as :

$result3 = array_map(function($v1, $v2){
    return $v1 + $v2;
}, $result1, $result2);

$result4 = array_map(function($v1, $v2){
    return $v1 * $v2;
}, $result1, $result2);

Code online in here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading