【问题标题】:How to loop through this and echo this如何循环并回显这个
【发布时间】:2016-06-15 07:09:20
【问题描述】:

localhost/getphp.php?item[]=Bag&Price=250&Quantity=30&item[]=Charger&Price=300&Quantity=35

 var_dump($_GET['item']);

 array (size=2)
    0 => string 'Bag' (length=3)
    1 => string 'Charger' (length=7)

但我想要每个项目的所有值及其值。

【问题讨论】:

  • 改为设计一个分组名称:item[0][name]=bag&item[0][price]=250 等等
  • 如何循环遍历这个?
  • 附加提示:您需要设置表单以使您的 url 采用该结构,以便在提交时采用该格式,然后剩下的就是一个简单的 foreach

标签: php foreach query-string


【解决方案1】:

您无法通过不绑定传递这些值来获取每个产品的所有数据。

使用这个:

localhost/getphp.php?item[0][item]=Bag&item[0][price]=100&item[0][quantity]=1&item[1][item]=Purse&item[1][price]=250&item[1][quantity]=6

【讨论】:

    【解决方案2】:

    如其他答案中所述,您需要按如下方式绑定值

    localhost/getphp.php?item[0]['name']=Bag&item[0]['price']=250&item[0]['quantity']=30&item[0]['name']=Charger&item[0]['price']=300&item[0]['quantity']=35
    

    然后你可以遍历它并通过索引获取值。

    <?php
     $item_array = $_GET['item'];
    
     for($i=0; $i<count($item_array); $i++){
      echo 'Name: ' . $item_array[$i]['name']. '</br>';
      echo 'Price: ' . $item_array[$i]['price']. '</br>';
      echo 'Quantity: ' . $item_array[$i]['quantity']. '</br>';
     }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2011-12-22
      相关资源
      最近更新 更多