2016-06-22 124 views
-1

使用Medoo當高級選擇像這樣:CSV從嵌套陣列

$medoo->query('SQL Goes HERE')->fetchAll(); 

結果列於下面的結構中返回:

Array ( 
    [0] => Array ( 
     [Column 0 name] => "VALUE OF Record 0, Column 0" 
     [0] => "VALUE OF Record 0, Column 0" 
     [Column 1 name] => "VALUE OF Record 0, Column 1" 
     [1] => "VALUE OF Record 0, Column 1" 
     ... 
     ... 
     [Column 99 name] => "VALUE OF Record 0, Column 99" 
     [99] => "VALUE OF Record 0, Column 99" 
     ) 
    [1] => Array ( 
     [Column 0 name] => "VALUE OF Record 1, Column 0" 
     [0] => "VALUE OF Record 1, Column 0" 
     [Column 1 name] => "VALUE OF Record 1, Column 1" 
     [1] => "VALUE OF Record 1, Column 1" 
     ... 
     ... 
     [Column 99 name] => "VALUE OF Record 1, Column 99" 
     [99] => "VALUE OF Record 1, Column 99" 
     ) 
    ... 
    ... 
    ... 
    [99] => Array ( 
     [Column 0 name] => "VALUE OF Record 99, Column 0" 
     [0] => "VALUE OF Record 99, Column 0" 
     [Column 1 name] => "VALUE OF Record 99, Column 1" 
     [1] => "VALUE OF Record 99, Column 1" 
     ... 
     ... 
     [Column 99 name] => "VALUE OF Record 99, Column 99" 
     [99] => "VALUE OF Record 99, Column 99" 
     ) 
) 

每個陣列具有兩個締合鍵和和索引鍵值對出於某種原因,不是100%爲什麼他們這樣構建它。有關如何刪除所有索引結果並僅保留關聯數組的任何建議?

+1

與PDO嘗試'$行= $ stmt->取(PDO :: FETCH_ASSOC );'儘管我沒有看到你的查詢在哪裏,所以只是猜測你使用的驅動程序。 – ArtisticPhoenix

+0

'我不知道如何fputcsv實際上工作,當涉及到多維數組',據我所知,它不應該,你應該通過foreach循環頂層,並輸出'$行'文件。通常情況下,這是如何完成的。 – ArtisticPhoenix

+0

我沒有使用PDO,我正在使用medoo:http://medoo.in – TheRealMrCrowley

回答

1

使用fetchall需要正確填寫:

$medoo->query('Query String')->fetchAll(PDO::FETCH_ASSOC); 
0

您可能需要沿着這些方向。這是假設你勾勒的數據類型是精確

$columns = array(array()); 
foreach ($arr[0] as $key => $value) { 
    if (!is_int($key)) {   
     $columns[0][] = $value; 
    } 
} 

$rows = array(); 
foreach ($arr as $results) { 
    $thisRow = array(); 
    foreach ($results as $key => $value) { 
     if (is_int($key)) { 
      $thisRow[] = $value; 
     } 
    } 
    $rows[] = $thisRow; 
} 

$output = array_merge($columns, $rows); 
+0

基於ArtisticPoenix的建議,我用了一些非常相似的東西。有時候我只是忽略了明顯的 – TheRealMrCrowley