Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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_Arrays_Csv - Fatal编程技术网

在php中读取CSV时在所有数组中实现第一个数组键

在php中读取CSV时在所有数组中实现第一个数组键,php,arrays,csv,Php,Arrays,Csv,我得到这样的输出- if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) { $files = fopen($uploadPath, 'r'); while (($line = fgetcsv($files, 0, ",")) !== FALSE) { //$line = array_combine($line,$line); try but not wo

我得到这样的输出-

if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
            $files = fopen($uploadPath, 'r');
            while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
            //$line = array_combine($line,$line); try but not working
            pr($line);
            }
}
但我需要这样的输出-

Array
(
    [Company Name] => Company Name
    [Primary First] => Primary First
    [Primary Last] => Primary Last
)
Array
(
    [0] => Naturfrisk Energy Bar
    [1] => Aksel
    [2] => Romer
)
Array
(
    [0] => The Code Devloper
    [1] => vikas
    [2] => Tyagi
)

我认为您应该为以后的数组存储由
fgetcsv
返回的第一个数组,因为第一行是标题行,您希望在随后返回的数组中使用该行

所以像这样的东西会有用的

Array
(
    [Company Name] => Company Name
    [Primary First] => Primary First
    [Primary Last] => Primary Last
)
Array
(
    [Company Name] => Naturfrisk Energy Bar
    [Primary First] => Aksel
    [Primary Last] => Romer
)
Array
(
    [Company Name] => The Code Devloper
    [Primary First] => vikas
    [Primary Last] => Tyagi
)
您可以使用,但首先必须有标题:

if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
            $files = fopen($uploadPath, 'r');
            $lineNo = 1;
            while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
                if($lineNo == 1)
                   $arrHeader = $line;
                else
                   $line = array_combine($arrHeader, $line);

                pr($line);
                $lineNo++;
            }
}
和输出:

$headers = fgetcsv($files, 0, ",");

while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
    $line = array_combine($headers, $line);
    print_r($line);
}
Array
(
    [Company Name] => Naturfrisk Energy Bar
    [Primary First] => Aksel
    [Primary Last] => Romer
)