0

我有一个从一些代码生成的数组:

if (($handle = fopen('/var/tmp/file.tsv', 'r')) !== FALSE) {
    $nn = 0;
    while (($data = fgetcsv($handle, 1000, '/t')) !== FALSE) {
        $targetData = array();                 
        foreach($targetColumns as $column)      
        $targetData[] = $data[$column]; 
        $csvarray[$nn] = $targetData;
        $nn++;
    }
    fclose($handle);
}

这给了我这个输出:

Array
(
  [0] => Array
    (
        [0] => "92834029"
    )
  [1] => Array
    (
        [0] => "32926154"
    )
  [2] => Array
    (
        [0] => "84892302"
    )
  [3] => Array
    (
        [0] => "10499507"
    )
)

我如何摆脱多维数组,而是让它成为一个干净的简单父数组?

Array
(
  [0] => 92834029
  [1] => 32926154
  [2] => 84892302
  [3] => 10499507
)
4

2 回答 2

4

In your case changing it to the following will work:

if (($handle = fopen('/var/tmp/file.tsv', 'r')) !== FALSE) {        
    while (($data = fgetcsv($handle, 1000, '/t')) !== FALSE) {              
        foreach($targetColumns as $column) {          
            $csvarray[] = $data[$column];
        }
    }
    fclose($handle);
}

Took the liberty of removing unnecessary stuff too.

于 2013-11-07T19:55:57.423 回答
2

尝试这样做:

if (($handle = fopen('/var/tmp/file.tsv', 'r')) !== FALSE) {
  $nn = 0;
  while (($data = fgetcsv($handle, 1000, '/t')) !== FALSE) {
    foreach($targetColumns as $column)      
    $targetData = $data[$column]; 
    $csvarray[$nn] = $targetData;
    $nn++;
  }
  fclose($handle);
}

无论如何,$targetData总是有点“无效” 。= array()或者直接用$csvarray[$nn] = $data[$column];.

于 2013-11-07T19:56:37.537 回答