array_combine接收一个布尔值但数据另有说明

时间:2015-08-21 17:06:50

标签: php arrays

所以我编写了下面的函数来解析CSV文件,然后将其传回控制器。

问题是array_combine函数接收false作为第二个参数但是当我打印出第二个参数时,我得到了一个预期的数据数组。

public function parseData($meta, $filename = 'file.csv')
{
    $fileContents = fopen($meta . $filename, 'r');
    $result = fgetcsv($fileContents, 1000, "\t", "\n");

    $allRowResult = array();
    while (!feof($fileContents)) {
        $rows[] = fgetcsv($fileContents, 1000, "\t", "\n");
    }

    foreach ($rows as $row) {

        $newArray = array_combine($result, $row);
        array_push($allRowResult, $newArray);


        echo "<pre>";
        print_r($row);
        echo "</pre>";


        echo "<pre>";
        print_r($result);
        echo "</pre>";
    }

    return $allRowResult;

}

print_r($ row)返回

Array
(
[0] => BBB
[1] => 10000001
[2] => 10000001
[3] => 06/15/2015
[4] => 07/06/2015
[5] => $0.00
[6] => $0.00
[7] => $40.14
[8] => $40.14
[9] => First Last
[10] => 123 Street
[11] => NULL
[12] => City
[13] => Province
[14] => Postal
[15] => 
[16] => P
[17] => 
[18] => filename.pdf
[19] => filename
[20] => 
[21] => 
[22] => 
[23] => 
[24] => 
[25] => 
[26] => 
[27] => 
[28] => 
[29] => 
[30] => 
[31] => 0
[32] => 1
[33] => 3
[34] => 1
[35] => 3
[36] => 11312
[37] => 5649

2 个答案:

答案 0 :(得分:0)

array_combine将数组作为第二个参数,$ row不是数组。

definition of array_combine

  

array array_combine(array $ keys,array $ values)

答案 1 :(得分:0)

所以我最终换掉了这个

的while循环
while(($line = fgetcsv($fileContents,1000,"\t", "\n")) !== FALSE){
       $rows[] = $line;
    }

立即对问题进行排序。