【问题标题】:php loop through array within array - output array value and keyphp循环遍历数组中的数组 - 输出数组值和键
【发布时间】:2013-12-22 14:58:48
【问题描述】:

我必须保留以下数组结构,因为它是现有网站:

$label_options = array(
    "label_size" => 'A4',
    "label_position" => 'Top',
    "label_qty" => 50
);

$ink_options = array(
    "black" => 1,
    "colour" => 0
);

$item = array(
    "item_number" => 12546518,
    "item_type" => 'Canon',
    "item_label_options" => $label_options,   
    "item_ink_options" => $ink_options,
    "item_qty" => 2,      
    "item_price" => 13.99,
    "item_total" => 26.98,
    "item_delivery" => 2.49
);

if (isset($_SESSION['cart'])) {
    $_SESSION['cart'][$unique] = $item;
} else {
    $_SESSION['cart'] = array($unique => $item);
}

我现在希望能够输出数组内的值和数组内的数组。我可以使用以下内容:

foreach ($_SESSION['cart'] as $unique => $item) {
    print 'Item Number = '.$item['item_number'].'<br />';
    print 'Qty = '.$item['item_qty'].'<br />';
    print 'Price = '.$item['item_price'].'<br />';

    foreach ($item['item_label_options'] as $key => $option) {
       print $option['label_size'].'<br />';
       print $option['label_position'].'<br />';
       print $option['label_qty'].'<br />';
    }
}

它成功输出了第一个循环,但没有输出第二个循环——只输入每个循环的第一个字母:

Item Number = 12546518
Qty = 1
Price = 7.99

A // should be A4
T // should be Top
5 // should be 50

还有一种方法可以让我循环并输出所有值,而无需实际编写'Item Number = ... 它可以只输出每个键及其值,因为我并不总是知道每个键有多少以及每个键的名称是什么?

【问题讨论】:

    标签: php arrays loops multidimensional-array foreach


    【解决方案1】:

    为什么不只打印 $key 和 $value,这应该打印你想要的:http://jp1.php.net/manual/de/control-structures.foreach.php

    【讨论】:

      【解决方案2】:

      您错误地访问了 $_SESSION['cart'] 中的 $label_options 数组。内...

      foreach ($item['item_label_options'] as $key => $option) {
         print $option['label_size'].'<br />';
         print $option['label_position'].'<br />';
         print $option['label_qty'].'<br />';
      }
      

      ...$option 是数组值不是数组本身。您可以通过回显 $key 和 $option 轻松验证这一点。当您尝试打印 $option['label_qty'] 时,PHP 本质上将字符串视为数组,给您第一个字母。

      您可以通过以下方式按键访问项目:

      foreach ($_SESSION['cart'] as $unique => $item) {
          echo 'Item Number = '.$item['item_number'].'<br />';
          echo 'Qty = '.$item['item_qty'].'<br />';
          echo 'Price = '.$item['item_price'].'<br />';
          echo $item['item_label_options']['label_size'];
          echo $item['item_label_options']['label_position'];
          echo $item['item_label_options']['label_qty'];
      }
      

      【讨论】:

      • 感谢您的回答 Dave,有什么办法可以让我遍历所有 item_label_options,因为我并不总是知道有多少个以及每个人叫什么?
      • @odd_duck 我的回答可以满足您的需求。您只需要回显 $option 本身的值。
      【解决方案3】:

      在您的内部foreach 循环中,键label_sizelabel_positionlabel_qty 不存在。当一个键不存在时,我猜它默认为 0,因此只给你第一个字母。这将产生你想要的:

      foreach ($_SESSION['cart'] as $unique => $item) {
          print 'Item Number = '.$item['item_number'].'<br />';
          print 'Qty = '.$item['item_qty'].'<br />';
          print 'Price = '.$item['item_price'].'<br />';
          foreach ($item['item_label_options'] as $key => $option) {
             print $option.'<br />';
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        • 1970-01-01
        • 2011-05-23
        • 2012-11-14
        相关资源
        最近更新 更多