Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用CSV的第一行作为数组键_Php_Csv_Fgetcsv - Fatal编程技术网

Php 使用CSV的第一行作为数组键

Php 使用CSV的第一行作为数组键,php,csv,fgetcsv,Php,Csv,Fgetcsv,我将如何使用正在使用fgetcsv()处理的CSV的第一行作为将每行放入的数组的键?因此,我可以使用$line['order-id']和$line['purchase-date']来代替元素键$line[0],$line[6]等 这只是因为有时我使用的数据源会切换行,所以它们需要由列名引用,以确保我选择了正确的数据。以下是我当前使用的代码: // create a new array that will be a child to $array for each order, contains

我将如何使用正在使用fgetcsv()处理的CSV的第一行作为将每行放入的数组的键?因此,我可以使用
$line['order-id']
$line['purchase-date']
来代替元素键
$line[0]
$line[6]

这只是因为有时我使用的数据源会切换行,所以它们需要由列名引用,以确保我选择了正确的数据。以下是我当前使用的代码:

// create a new array that will be a child to $array for each order, contains details.
while (($line = fgetcsv($file,'','  ','\n')) !== FALSE) {
    // count (for email later on)
    if(!isset($count)) {
        $count = count($line);
    }

    // add to new array (subarray)
    $individualarray = array(
        'order-id' => $line[0],
        'buyer-name' => ucwords($line[11]),
        'purchase-date' => $line[6],
        'email-address' => $line[10]
    );
    $num++;
    // add to the array of all orders
    $array[$num] = $individualarray;
}
//remove first record from array, these are the headers.
unset($array[1]);

// close file
fclose($file);
以下是我正在使用的CSV示例:

amazon-order-id merchant-order-id   purchase-date   last-updated-date   order-status    fulfillment-channel sales-channel   order-channel   url ship-service-level  product-name    sku asin    item-status quantity    currency    item-price  item-tax    shipping-price  shipping-tax    gift-wrap-price gift-wrap-tax   item-promotion-discount ship-promotion-discount ship-city   ship-state  ship-postal-code    ship-country    promotion-ids 
xxxxx-xxxxx-xxxxx   xxxxx-xxxxx-xxxxx   2015-09-17T09:27:35+00:00   2015-09-17T09:27:37+00:00   Pending Amazon  Amazon.es           Standard    Some product name   xx-xxxx-xxxx    xxxxxxx Unshipped   1   EUR 19.99                               Huelva  Huelva  xxxxx   ES      
xxxxx-xxxxx-xxxxx   xxxxx-xxxxx-xxxxx   2015-09-17T17:35:28+00:00   2015-09-17T17:35:29+00:00   Pending Amazon  Amazon.co.uk            Expedited   Another Product Name    xx-xxxx-xxxx    xxxxxxx Unshipped   1   GBP 14.99                               Eastleigh   Hampshire   xxxx xxx    GB  
您的
fgetcsv()
看起来不正确。您需要
“\t”
作为选项卡,或者
作为3个空格。我不知道你在尝试换行时做了什么
\n

获取第一行并合并:

// get first line
$keys = fgetcsv($file, 0, "\t");

while (($line = fgetcsv($file, 0, "\t")) !== FALSE) {
    $array[] = array_combine($keys, $line);
}
或仅针对某些列:

$array[] = array(
                    $keys[0]  => $line[0],
                    $keys[11] => ucwords($line[11]),
                    $keys[6]  => $line[6],
                    $keys[10] => $line[10]
);

还请注意,不必硬编码列号。尝试以下代码后,返回了大量布尔(false)值。我不知道你的档案是什么样子的。它的格式不正确,或者您的
fgetcsv()
参数不正确。很抱歉,我添加了一些正在编辑的示例数据。我以为你的
fgetcsv()
找到了。