我乘两个数组是一个挑战。这就是我打算做的。
数组1([0] => 2 [1] => 2)数组2([0] => 8000.00 [1] => 1234.00)每次我乘这一点,将其分解为4个,并返回一个结果,因为这
阵列([0] => 16000 [1] => 16000 [2] => 2468 [3] => 2468)然而,当我通过单一它得到正确的单个数据。这里是我的code,我将AP preciate任何帮助,我可以得到的。谢谢
$ =总阵列();的foreach($ office_price为$价格){ 的foreach($ office_quantity为$数量){ $总[] = $价格* $数量; }}解决方案
您遍历两个数组,所以你得到的每一个值的两倍。
如你所知,一个阵列建成使用键和值。
使用您的foreach到那个程度:
$ =总阵列();的foreach($ office_price为$关键=> $价格){ $总[] = $价格* $ office_quantity [$关键];}您只需要循环一个阵列,并且通过使用具有相同密钥对第二阵列的价值,你得到正确的结果。
I have a challenge multiplying two arrays. this is what i intend doing
Array1 ( [0] => 2 [1] => 2 ) Array2 ( [0] => 8000.00 [1] => 1234.00 )Every time i multiply this it breaks it down into 4 and returns a result as this
Array ( [0] => 16000 [1] => 16000 [2] => 2468 [3] => 2468 )However when i pass single a single data it gets it right. Here is my code, i'll appreciate any help i can get. Thanks
$total = array(); foreach($office_price as $price){ foreach($office_quantity as $quantity){ $total[] = $price * $quantity; } }解决方案
You loop through both arrays, so you get every value twice.
As you know, an array is built up using keys and values.
Use your foreach to that extent:
$total = array(); foreach ($office_price as $key=>$price) { $total[] = $price * $office_quantity[$key]; }You only need to loop one array, and by using the value from the second array with the same key, you get the proper result.
更多推荐
相乘在PHP两个数组
发布评论