从嵌套数组(foreach循环)中得到PHP回声数据。

问题描述 投票:0回答:1

我在从一个嵌套数组中呼应数组数据时遇到了问题,我的数组(单个订单)看起来像这样......

Array
(
    [buyer_name] => Mr Smith
    [buyer_phone] => 01234 567890
    [buyer_email] => [email protected]
    [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>";

}

毫无进展,请问我做错了什么?

EDIT TO ADD...

如果我不能用这种方式呼应数组,那么这将导致我使用大量的 "if "语句(一个顺序可能有100多个项目),下面只是针对 "标题 "的例子,我必须对嵌套数组中的每个项目重复几百次,一定有更好的方法来实现这个目标吗?

    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
php arrays nested echo
1个回答
1
投票

首先,数组键必须用引号。

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

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

通常你的PHP版本会抛出一个警告。

警告:使用未定义的常量 item_t 使用未定义的常量 item_title - 假定为'item_title'(这将在以后的 PHP 版本中抛出一个 Error)。

你可以对数组进行迭代。

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/>";
  }
}

. 连接两个字符串。你不需要两个echo-commands。

注意措辞,这样可以使代码更易读。

foreach (<plural> as <singular>)
© www.soinside.com 2019 - 2024. All rights reserved.