-1

我正在对 PHP 进行 CSV 操作,我发现一个函数在 stackoverflow 上做得很好,除了它返回我的数组的值作为使数组难以操作的键。

array(6) {
       [0]=>
       array(2) {
         ["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
         string(66) "Aliens: Colonial Marines: Limited Edition (Xbox 360)  - EU Version"
         ["€ 14.34"]=>
         string(8) "€ 7.17"
       }
       [1]=>
       array(2) {
         ["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
         string(46) "Arcania: Gothic 4 - Special Edition (Xbox 360)"
         ["€ 14.34"]=>
         string(8) "€ 9.56"
       }
       [2]=>
       array(2) {
         ["Aliens: Colonial Marines: Limited Edition (Xbox 360)"]=>
         string(29) "Assassin's Creed 3 (Xbox 360)"
         ["€ 14.34"]=>
         string(9) "€ 15.54"
       }
   [MORE]
     }

有什么方法可以将数组更改为此以及如何更改??:

         array(6) {
       [0]=>
       array(2) {
         [0]=>
         string(66) "Aliens: Colonial Marines: Limited Edition (Xbox 360)  - EU Version"
         [1]=>
         string(8) "€ 7.17"
       }
       [1]=>
       array(2) {
         [0]=>
         string(46) "Arcania: Gothic 4 - Special Edition (Xbox 360)"
         [1]=>
         string(8) "€ 9.56"
  [MORE]
       }

谢谢

编辑:

     function parse_csv_file($csvfile) {
         $csv = Array();
         $rowcount = 0;
         if (($handle = fopen($csvfile, "r")) !== FALSE) {
             $max_line_length = defined('MAX_LINE_LENGTH') ? MAX_LINE_LENGTH : 10000;
             $header = fgetcsv($handle, $max_line_length);
             $header_colcount = count($header);
             while (($row = fgetcsv($handle, $max_line_length)) !== FALSE) {
                 $row_colcount = count($row);
                 if ($row_colcount == $header_colcount) {
                     $entry = array_combine($header, $row);
                     $csv[] = $entry;
                 }
                 else {
                     error_log("csvreader: Invalid number of columns at line " . ($rowcount + 2) . " (row " . ($rowcount + 1) . "). Expected=$header_colcount Got=$row_colcount");
                     return null;
                 }
                 $rowcount++;
             }
             //echo "Totally $rowcount rows found\n";
             fclose($handle);
         }
         else {
             error_log("csvreader: Could not read CSV \"$csvfile\"");
             return null;
         }
         return $csv;
     }
4

2 回答 2

1

试试这$arr_new是输出你想要的数组的最终数组

 $arr = array(
        "0"=>array(
            "Aliens: Colonial Marines: Limited Edition (Xbox 360)"=>"Aliens: Colonial Marines: Limited Edition (Xbox 360)  - EU Version",
            "€ 14.34"=>"€ 7.17"
        ),
        "1"=>array(
            "Aliens: Colonial Marines: Limited Edition (Xbox 360)"=>"Arcania: Gothic 4 - Special Edition (Xbox 360)",
            "€ 14.34"=>"€ 9.56"
        ),
        "2"=>array(
            "Aliens: Colonial Marines: Limited Edition (Xbox 360)"=>"Assassin's Creed 3 (Xbox 360)",
            "€ 14.34"=>"€ 15.54"
        ),
    );
    foreach($arr as $key => $value)
    {
        $arr_new[] = array_values($value);
    }
    echo "<pre>";
    print_r($arr_new);
于 2013-10-04T10:19:39.967 回答
0

您正在寻找array_values函数。

array_values() 返回数组中的所有值并以数字方式索引数组。

使用 foreach 或array_walk在所有具有您不想要的索引的数组上运行它。

于 2013-10-04T10:10:18.943 回答