亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP數組合并,忽略某些重復的鍵,讓它們包含在內部創建的數組中

PHP數組合并,忽略某些重復的鍵,讓它們包含在內部創建的數組中

PHP
素胚勾勒不出你 2022-12-11 16:11:50
我將具有相同設置內部數組名稱的數組合并在一起,將鍵值更改為訂單號,然后為未與此代碼重復的項目創建更多內部數組...function readCSV($csvFile)    {        $line_of_text = [];        $file_handle = fopen($csvFile, 'r');        //skip csv headers        //fgetcsv($file_handle);        //fgetcsv($file_handle);        fgetcsv($file_handle);          while (!feof($file_handle)) {            $tmp = fgetcsv($file_handle, 1024);            if (isset($line_of_text[$tmp[0]])) {                foreach ($tmp as $k => $v) {                    if (array_key_exists($k, $line_of_text[$tmp[0]])) {                        if (!is_array($line_of_text[$tmp[0]][$k])) {                            $kVal = $line_of_text[$tmp[0]][$k];                            $line_of_text[$tmp[0]][$k] = [];                            $line_of_text[$tmp[0]][$k][] = $kVal;                        }                        $line_of_text[$tmp[0]][$k][] = $v;                        $line_of_text[$tmp[0]][$k] = array_unique($line_of_text[$tmp[0]][$k]);                        $line_of_text[$tmp[0]][$k] = array_filter($line_of_text[$tmp[0]][$k]);                        if (count($line_of_text[$tmp[0]][$k]) == 1) {                            $line_of_text[$tmp[0]][$k] = array_values($line_of_text[$tmp[0]][$k]);                            $line_of_text[$tmp[0]][$k] = $line_of_text[$tmp[0]][$k][0];                        }                        if (empty($line_of_text[$tmp[0]][$k])) {                            $line_of_text[$tmp[0]][$k] = null;                        }                    } else {                        $line_of_text[$tmp[0]][$k] = null;                    }                }                $line_of_text[$tmp[0]][0] = $tmp[0];            } else {                $line_of_text[$tmp[0]] = $tmp;            }        }我怎樣才能從“刪除重復項”部分中排除某些內容,以便它們像上一個示例一樣在內部數組中重復?這是必需的,因為它們直接鏈接到具有內部數組的其他項目,因此如果例如 item1 內部數組現在有 6 個項目,那么 qty 現在也需要在內部數組中包含所有 6 個項目,即使它們是相同的。
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

您當前的代碼適用于兩種情況:

  1. 按原樣讀取所有數據,不加修改

  2. 讀取所有數據并執行通用修改

由于您需要的是條件修改,因此您最好手動創建數組的結構。這樣做會增加另一個好處:代碼清晰度。您應該始終爭取描述性代碼,因此使用描述性關聯鍵構建數組將使代碼的意圖更加清晰。

基于示例數據的建議解決方案(您應該根據您的特定需求定制的粗略草圖):

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] = [

                '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($orders[$orderId]['items'] as $item)


查看完整回答
反對 回復 2022-12-11
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號