相乘在PHP两个数组

编程入门 行业动态 更新时间:2024-10-07 22:26:48
本文介绍了相乘在PHP两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我乘两个数组是一个挑战。这就是我打算做的。

数组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两个数组

本文发布于:2023-11-30 13:36:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   两个   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!