【问题标题】:PHP multidimensional array with logical arguments (some brain twisting)带有逻辑参数的 PHP 多维数组(有些脑筋急转弯)
【发布时间】:2011-05-06 20:16:49
【问题描述】:

问题: 我有一个来自 magento 的变量,它存储在模型类中,可以作为

$productArray[] = array();
foreach ($order->getAllItems() as $item) {
    $productArray[] = array(
        "product"  => $item->getName(),
        "qty"   => $item->getQtyOrdered(),
        "amount" => $item->getPrice(),
    );
}

这是 print_r $productArray[] 的值:
示例输出 1:

array(1) {
  [0]=>
  array(3) {
    ["product_name"]=>
    string(12) "Test Product"
    ["product_qty"]=>
    string(6) "2.0000"
    ["product_price"]=>
    string(7) "12.0000"
  }
}


示例输出 2:

array(2) {
  [0]=>
  array(3) {
    ["product_name"]=>
    string(12) "Test Product"
    ["product_qty"]=>
    string(6) "2.0000"
    ["product_price"]=>
    string(7) "12.0000"
  }
  [1]=>
  array(3) {
    ["product_name"]=>
    string(6) "Test 2"
    ["product_qty"]=>
    string(6) "5.0000"
    ["product_price"]=>
    string(7) "22.0000"
  }
}

你怎么能这样?(应该这样打印)
如果输出1:最终输出1

<input type="hidden" name="product" value="Test Product" />
<input type="hidden" name="amount" value="24.00" />


如果输出 2:最终输出 2

<input type="hidden" name="product1" value="Test Product" />
<input type="hidden" name="amount1" value="24.00" />
<input type="hidden" name="product2" value="Test 2" />
<input type="hidden" name="amount2" value="110.00" />

金额值将被输入 product_price * product_qty

玩得开心:) 这只是一个虚拟问题,但这可能对其他人有所帮助

【问题讨论】:

    标签: php arrays multidimensional-array logic


    【解决方案1】:

    像这样:

    <?php
    foreach($productArray as $i => $product){
      $index = count($productArray) == 1 ? "" : $i; //So we don't have index when only 1 element
      $amount = $product['product_price'] * $product["product_qty"];
      $name = $product['product_name'];
    ?>
      <input type="hidden" name="product<?php echo $index; ?>" value="<?php echo $name;?>" />
      <input type="hidden" name="amount<?php echo $index;?>" value="<?php echo $amount;?>" />
    <?php
    }
    ?>
    

    希望这会有所帮助。干杯

    【讨论】:

      【解决方案2】:

      不确定magento,但在正常的php中它会是:

      <?php
        $productArray = array(
          array(
            "product_name" => "Test Product",
            "product_qty" => "2.0000",
            "product_price" => "12.0000"
      ),
          array(
        "product_name"=> "Test 2",
            "product_qty"=>"5.0000",
            "product_price"=>"22.0000"
          )
        );
      
        foreach($productArray as $v) {
          echo '<input type="hidden" name="product" value="'.$v["product_name"].'" />';
          echo '<input type="hidden" name="amount" value="'.($v["product_qty"]*$v["product_price"]).'" />';
        }
      ?>
      

      【讨论】:

        【解决方案3】:

        在php中制作表单字段数组时,使用“product[$id]”作为名称,那么php会在$_POST中反刍一个整齐的数组。你甚至可以做 name="product[$id][price]" 并且你会得到一个二维数组。

        请注意,如果您的网店信任隐藏的表单数据在结帐时在购物车中拖曳(就像您似乎所做的那样),那么您就有一个巨大的安全漏洞,所以请发布网址,我想免费订购东西!

        【讨论】:

        • 哈哈哈......正如我所说的“这只是一个虚拟问题”。我提出这个问题是因为我对他的会话输出感到好奇。并探索多维数组。 :)
        猜你喜欢
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 2017-05-30
        • 2014-08-03
        • 2011-02-14
        相关资源
        最近更新 更多