dhara gosai
dhara gosai

Reputation: 92

how to multiply two array value without loop in laravel

I have tried to multiply $qty_test and $get_ltr also that data type is an integer, but that give me error

Unsupported operand types

what is issued in the code.

$qty_test=explode(",",$request->input('qty'));

foreach ($part_id as $part_ids) {
     $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();
     $ltr[] = $get_ltr->ltr;
     $total_ltr= $qty_test * $ltr; 
}

that gives me error how to multiply it please help me.

Upvotes: -1

Views: 1315

Answers (6)

Rajat Masih
Rajat Masih

Reputation: 583

To get all row multiplication

$qty_test=explode(",",$request->input('qty'));

foreach ($part_id as $part_ids) {
     $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();
     $ltr = $get_ltr->ltr;
     $total_ltr[]= $qty_test * $ltr; 
}
print_r($total_ltr);

Upvotes: 0

Jignesh Joisar
Jignesh Joisar

Reputation: 15175

you need to used array_sum method here

first of the sum of both array one by one look like this..

for example

$arrayOne = [5,10,15];    //total is 30
$arrayTwo = [5,10];       //total is 15  
$sumOfTwoArray = array_sum($arrayOne) * array_sum($arrayTwo); 

output is(30 *15) : 450

now in your code modify like that

$qty_test=explode(",",$request->input('qty'));

foreach ($part_id as $part_ids) {
     $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();
     $ltr[] = $get_ltr->ltr;
 }
$total_ltr= array_sum($qty_test) * array_sum($ltr);  //used array_sum method here in both array

only get multiplication of each row qty and ltr then used like that

 $array = [5,10,15];
 $ltr = ['5','10'];

 $array1 =  array_map(function ($v) use ($array) {
           info("v ;- ".$v);
         return array_map(function ($a) use ($v){
                info("a :- ".$a." v :- ".$v);
                return   $a * $v;
          },$array);
  },$ltr);

  //info("total is :- ".print_r($array1,true));

Upvotes: 0

ManojKiran
ManojKiran

Reputation: 6351

Sorry for the late reply a while ago i have the situation to multiple all the values in array and finally created the function

for eg ;

$multiplicationArrays = ['10','10','50'];

is my array

and my expected result is 5000

so i have created the function

function multiplyArray($numbersArray='')
    {
        //defining the error messages
        $errorMessage = "Minimum Two Numbers is required";
        //check if atleast two numbers in array
        $isMultiplicable = count($numbersArray);
        if ($isMultiplicable < 2 ) 
        {
            //if fails return the errormessage
           return $errorMessage;
        }
        else
        {
            //initializing the resilt as one for the first time
            $result = 1;
           foreach ($numbersArray as $numbersArray)
            {
                //on the each multiple an store it in the result varialbe
                $result *= $numbersArray;
            }
            //return the result
            return $result;
        }
    }

and finnaly pass the array inside the function

echo multiplyArray($multiplicationArrays);

and the result will be as expected 5000

hope it helps

Upvotes: 0

Syamsoul Azrien
Syamsoul Azrien

Reputation: 2752

#1 - If you want to get each multiplication for each value, follow this code:

$qty_test=explode(",",$request->input('qty'));

$ltr = [];
foreach ($part_id as $part_ids) {

   $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();

   array_push($ltr, $get_ltr->ltr);

}

$total_ltr = [];
foreach($qty_test as $i=>$val){
    array_push($total_ltr, $qty_test[$i] * $ltr[$i]);
}

....

....

#2 - But if you want to sum all values in first array and multiply it to the sum of values in second array, follow this code:

$qty_test=explode(",",$request->input('qty'));

$ltr = [];
foreach ($part_id as $part_ids) {

   $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();

   array_push($ltr, $get_ltr->ltr);

}

$total_ltr= array_sum($qty_test) * array_sum($ltr); 

.

array_sum() will sum all values in an array, for example

echo array_sum([23,14,45]);

// will return 82 (23 + 14 + 45)

.

For more detail, you can refer to Official PHP Documentation about array_sum()

Upvotes: 0

MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1570

You are forgotten $qty_test have array value after you use explode(). So you have to use array_sum() to sum all of your $qty_test array and multiply this with your respective value.

Try this

$qty_test=explode(",",$request->input('qty'));

foreach ($part_id as $part_ids) {
   $get_ltr = part::where('status',1)->where('part_no',$part_ids)->first();
   $ltr[] = $get_ltr->ltr;
}

$total_ltr= array_sum($qty_test) * array_sum($ltr); 
echo $total_ltr;

Hope this will help you

Upvotes: 0

user10186369
user10186369

Reputation:

You should try this:

$total_ltr = array_map(function () {
    return array_sum(func_get_args());
}, $qty_test, $get_ltr->ltr);

Upvotes: 0

Related Questions