第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

PHP數(shù)組合并,忽略某些重復(fù)的鍵,讓它們包含在內(nèi)部創(chuàng)建的數(shù)組中

PHP數(shù)組合并,忽略某些重復(fù)的鍵,讓它們包含在內(nèi)部創(chuàng)建的數(shù)組中

PHP
素胚勾勒不出你 2022-12-11 16:11:50
我將具有相同設(shè)置內(nèi)部數(shù)組名稱的數(shù)組合并在一起,將鍵值更改為訂單號(hào),然后為未與此代碼重復(fù)的項(xiàng)目創(chuàng)建更多內(nèi)部數(shù)組...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;            }        }我怎樣才能從“刪除重復(fù)項(xiàng)”部分中排除某些內(nèi)容,以便它們像上一個(gè)示例一樣在內(nèi)部數(shù)組中重復(fù)?這是必需的,因?yàn)樗鼈冎苯渔溄拥骄哂袃?nèi)部數(shù)組的其他項(xiàng)目,因此如果例如 item1 內(nèi)部數(shù)組現(xiàn)在有 6 個(gè)項(xiàng)目,那么 qty 現(xiàn)在也需要在內(nèi)部數(shù)組中包含所有 6 個(gè)項(xiàng)目,即使它們是相同的。
查看完整描述

1 回答

?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

您當(dāng)前的代碼適用于兩種情況:

  1. 按原樣讀取所有數(shù)據(jù),不加修改

  2. 讀取所有數(shù)據(jù)并執(zhí)行通用修改

由于您需要的是條件修改,因此您最好手動(dòng)創(chuàng)建數(shù)組的結(jié)構(gòu)。這樣做會(huì)增加另一個(gè)好處:代碼清晰度。您應(yīng)該始終爭(zhēng)取描述性代碼,因此使用描述性關(guān)聯(lián)鍵構(gòu)建數(shù)組將使代碼的意圖更加清晰。

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

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;

}

現(xiàn)在,您訂單中的所有項(xiàng)目都整齊地存儲(chǔ)為子數(shù)組,每個(gè)子數(shù)組僅包含該項(xiàng)目的特定數(shù)據(jù),這使得迭代變得非常容易:


foreach($orders[$orderId]['items'] as $item)


查看完整回答
反對(duì) 回復(fù) 2022-12-11
  • 1 回答
  • 0 關(guān)注
  • 107 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)