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

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

CSV 到關聯數組(一列作為鍵,另一列作為值)

CSV 到關聯數組(一列作為鍵,另一列作為值)

PHP
元芳怎么了 2023-05-12 15:34:18
我需要將 CSV 文件轉換為關聯數組,使用一列作為鍵,另一列作為值。例如,我有這個:Application,Warehouse,Item,UPC,MFUSA Item No.,"PRODUCT DESCR1","PRODUCT DESCR2",Warehouse ID,QtyINVENTORY:,Muscle Foods USA,1st STEP B12 LIQUID 16oz,673131100064,8890004,1st STEP B12 LIQUID 16oz,CHERRY CHARGE,11,29INVENTORY:,Muscle Foods USA,1st STEP B12 LIQUID 16oz,673131100316,8890007,1st STEP B12 LIQUID 16oz,TROPICAL BLAST,11,26INVENTORY:,Muscle Foods USA,1st STEP B12 LIQUID 16oz,673131100064,8890004,1st STEP B12 LIQUID 16oz,CHERRY CHARGE,12,6我必須使用該列UPC作為鍵和Qty值,以便數組看起來像..array(    '673131100064' => '29',    '673131100316 => '26',    '673131100064' => '6',);我已經嘗試了在谷歌和這里找到的幾種解決方案,但沒有一個接近我需要實現的目標。這個問題是類似的,但它是用 Python 編寫的。對不起,我對 PHP 的了解很少。你能指導我走向正確的方向嗎?
查看完整描述

3 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

使用array_map您可以解析 csv 數據并根據需要使用數組。例子:


// Parsing the data in csv file

$csv = array_map('str_getcsv', file('path/file.csv'));


/*At this point you already have an array with the data but

 * the first row is the header row in your csv file */


//remove header row

array_shift($csv);


$data = [];


//Walk the array and add the needed data into some another array

array_walk($csv, function($row) use (&$data) {

    $data[$row[3]] = $row[8];

});

就是這樣。


但是,您作為示例顯示的數據具有重復的 UPC。如果您想要一個具有結構的數組,您將覆蓋一些數據'UPC' => 'Qty'。數組中不能有重復的鍵。


如果您正在尋找的是獲取每個 UPC 的總數量,那么如果 UPC 密鑰已經存在,您只需將現有的數量添加到新的數量。


// Parsing the data in csv file

$csv = array_map('str_getcsv', file('file.csv'));


//remove header row

array_shift($csv);


$data = [];


//Walk the array and add the needed data into another array

array_walk($csv, function($row) use (&$data) {


    $data[$row[3]] = ($data[$row[3]] ? $data[$row[3]] + (int) $row[8] : (int) $row[8]);

});

或者更長但更清晰。


//Walk the array and add the needed data into another array

array_walk($csv, function($row) use (&$data) {


    if(!empty($data[$row[3]]))

    {

        $data[$row[3]] += (int) $row[8];

    }

    else {

        $data[$row[3]] = (int) $row[8];

    }

});


查看完整回答
反對 回復 2023-05-12
?
侃侃無極

TA貢獻2051條經驗 獲得超10個贊

下面代碼注釋中的解釋


$array = [];

// Open file "$file" and checking that it is not empty

if (($handle = fopen($file, "r")) !== false) {

    // loop on each csv line, stopping at end of file

    while (($data = fgetcsv($handle)) !== false) {

        // Excluding header row & checking data is not empty

        if ($data[0] !== 'Application' && !empty($data[0])) {

               // fgetcsv returns an array, on your example UPC is on key 3 and Qty on key 9

               $array[] = [$data[3] => $data[9]];

        }

     }

     fclose($handle);

 }


 return $array;

這里的鍵是硬編碼的,但也許你有辦法動態地放置它,這取決于你的代碼和工作流程。這只是一個簡單的(我希望)演示。


查看完整回答
反對 回復 2023-05-12
?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

我使用SplfileObject進行閱讀。然后第一行用作所有值的鍵。帶有列名的array_column現在可以用于所需的結果。

$csv = new SplFileObject('datei.csv');


$csv->setFlags(SplFileObject::READ_CSV?

? | SplFileObject::SKIP_EMPTY?

? | SplFileObject::READ_AHEAD?

? | SplFileObject::DROP_NEW_LINE

);


//Combine first row as key with values

$csvArr = [];

foreach($csv as $key => $row){

? if($key === 0) $firstRow = $row;

? else $csvArr[] = array_combine($firstRow,$row);

}


$arrQty = array_column($csvArr,'Qty','UPC');


查看完整回答
反對 回復 2023-05-12
  • 3 回答
  • 0 關注
  • 152 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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