PHP 从嵌套数组回显数据(foreach 循环)

PHP echo data from nested array (foreach loop)

我在从嵌套数组回显数组数据时遇到问题,我的数组(对于单个订单)如下所示...

Array
(
    [buyer_name] => Mr Smith
    [buyer_phone] => 01234 567890
    [buyer_email] => mygreatemail@fakeemail.com
    [items] => Array
        (
            [0] => Array
                (
                    [quantity] => 3
                    [item_title] => title 1
                    [custom_label] => JD3433
                    [item_variation] => Variation detail for title 1
                    [price] => £8.00
                )

            [1] => Array
                (
                    [quantity] => 1
                    [item_title] => title 2
                    [custom_label] => JD5544
                    [item_variation] => Variation detail for title 2
                    [price] => £7.00
                )

        )

)  

从此代码生成...

 function readCSV($csvFile)
    {
        $output = [];
        $fileHandle = fopen($csvFile, 'r');
        $header = fgetcsv($fileHandle);
        while (!feof($fileHandle)) {
            $fileRow = fgetcsv($fileHandle, 1024);
            $orderId = $fileRow[0];
            // skip this row if it's empty (the first field contains no id)
            if (empty($orderId)) {
                continue;
            }
            /*
              $fileRow[3] is "Buyer name", the first field that's present in one type of row
              (the one containing common properties of the order). By checking if it's empty,
              we identify the contents of the row - not empty means order row with common
              properties, empty means item row with specific item properties.
             */
            if (!empty($fileRow[3])) {
                // no need to repeat the id inside the array - it's already stored in the key
                $output[$orderId] = [
                    'sales_record' => $fileRow[0],
                    'order_number' => $fileRow[1],
                    'buyer_username' => $fileRow[2],
                    'buyer_name' => $fileRow[3],
                    // here you can continue explicitly adding any property you need
                ];
            } else {
                // add a new item entry
                $output[$orderId]['items'][] = [
                    'item_number' => $fileRow[20],
                    'item_title' => $fileRow[21],
                    'quantity' => $fileRow[24],
                    'price' => $fileRow[25],
                    // here you can continue explicitly adding any property you need
                ];
            }
        }
        fclose($fileHandle);

        return $output;
    }

我可以使用..

毫无问题地回显单个主数组数据
foreach($csv as $order) {
echo "Sales Record: "; echo $order[sales_record]; 
}

但是在尝试回显嵌套数组数据时遇到问题。

我在这里阅读了许多类似的问题并尝试过..

foreach($csv[$orderId]['items'] as $orderitems) {
echo $orderitems; echo "<br>";

}

运气不好,请问我做错了什么?

编辑添加...

如果我不能以这种方式回显数组,那么这将导致我使用大量 "if" 语句(一个订单中可能有 100 多个项目),下面只是一个例子对于"title",我必须为嵌套数组中的每个项目重复数百次,必须有更好的方法来实现这个??

    if ($order[items][0][item_title] != "") { echo $order[items][0][item_title]; echo "<br>"; } else {}
    if ($order[items][1][item_title] != "") { echo $order[items][1][item_title]; echo "<br>"; } else {}
    if ($order[items][2][item_title] != "") { echo $order[items][2][item_title]; echo "<br>"; } else {}

//and so on

首先,array-keys必须在引号内:

  • 否:$order[items][0][item_title]

  • 是:$order['items'][0]['item_title']

通常您的 PHP 版本会发出警告:

Warning: Use of undefined constant item_title - assumed 'item_title' (this will throw an Error in a future version of PHP)

您可以遍历数组:

foreach($order['items'] as $item) {
  echo $item['item_title']."<br/>";
}

如果您有一个嵌套数组(订单数组,每个订单都有一个项目数组),您可以嵌套 foreach。

$orders = readCSV('filename.csv');

foreach ($orders as $orderId=>$order) {    
  foreach($order['items'] as $item) {
    if (!empty($item['item_title'])) echo $item['item_title']."<br/>";
  }
}

. 连接两个字符串。你不需要两个回显命令。

注意措辞,使代码更具可读性。

foreach (<plural> as <singular>)