【问题标题】:I want to multiply two different arrays values that is coming through inputs我想将通过输入的两个不同的数组值相乘
【发布时间】:2019-10-30 13:45:15
【问题描述】:

我有一些输入接受通过 javascript 动态生成的数组值,因此用户可以添加更多商品名称、商品价格和商品数量,并通过 $_POST 将其发送到处理

像这样-----

<input type="text" name="item_name[]" id="item_name" placeholder="Item Name">

<input type="text" name="item_price[]" id="item_price" placeholder="Item Price">

<input type="text" name="quantity[]" id="quantity" placeholder="Quantity">

我只想将每个 item_price 乘以我的 item_quantity 喜欢 -----

如果我有 $item_price = 400;和 $quantity = 3;

然后我想要 $t = $item_price * $quantity 我从用户那里获得的每个值。

但我不明白如何遍历所有商品的价格和数量并将它们相乘。

请帮我解决这个问题,这是我唯一卡住的部分,我花了一整天的时间来找出尝试了不同的方法但得到了错误的值。

这是我的 PHP 代码

$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$quantity = $_POST['quantity'];

foreach ($item_name as $key => $i_n) {

       echo $i_n."<br>";
   }

foreach ($item_price as $key => $i_p) {

       echo $i_p."<br>";
   }

 foreach ($quantity as $key => $q) {

       echo $q."<br>";
   }

// I am outputting item name, item price,  quantity just to check

【问题讨论】:

  • 你只显示一个项目,多个项目的post数组结构是什么?
  • 使用诸如items[0][name] + items[0][quantity]items[1][name] + items[1][quantity] 之类的字段名称将使您能够以更有意义的方式对数据进行分组。

标签: php


【解决方案1】:

只需循环一组并将密钥与其他密钥匹配。

foreach ($item_name as $key => $value) {
    echo $item_name[$key].', '.
         $item_price[$key].', '.
         $quantity[$key]."<br>";
}

【讨论】:

  • 非常感谢您为我提供的其他工作,感谢您帮助我。你能告诉我,我的问题是菜鸟吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2019-02-15
  • 2022-11-25
  • 1970-01-01
  • 2022-06-16
  • 2019-06-08
相关资源
最近更新 更多