2012-11-28 71 views
0

我需要在PHP中將數組寫入.csv文件。Implode與PHP中的鍵陣列

例陣列:

$array = array(
    "name" => "John", 
    "surname" => "Doe", 
    "email" => "[email protected]" 
); 

通過使用implode(",", $array),我得到的結果是這樣的:
John,Doe,[email protected]

然而,我需要也寫入每個元素到該文件的密鑰。

所需的輸出是這樣的:
name:John,surname:Doe,email:[email protected]

我將如何實現這一目標?

+4

嘗試[序列化](http://php.net/manual/en/function.serialize.php)或[json encoding](http:// php。 net/manual/en/function.json-encode.php)數組。 – undefined

回答

10

嘗試此代碼:

$out = $sep = ''; 
foreach($array as $key => $value) { 
    $out .= $sep . $key . ':' . $value; 
    $sep = ','; 
} 
+6

我真的很喜歡'$ sep'的解決方案,以避免最終鍵值對之後的經典問題。 – TachyonVortex

+0

謝謝,工作完美。 –

1
$csv = ""; 

foreach($array as $key => $data) 
{ 
    // be sure to add " in your csv 
    $csv .= '"'.$key.':'.$data.'",'; 
} 

// and add a new line at the end 
$csv .= "\n"; 

echo $csv; 
+0

這將在每個鍵值對之後添加一個換行符,這不是傑克所要求的。此外,這將在最終的鍵值對之後留下尾隨逗號。 – TachyonVortex

+0

謝謝,現在修復。 –

-1
<?php 
$array = array(
"name" => "John", 
"surname" => "Doe", 
"email" => "[email protected]" 
); 
foreach($array as $key=>$data) { 
$output .= $comma . $key . ':' . $data; 
$comma = ','; 
} 

echo $output; 

?> 
0

的答案上述在端輸出一尾隨逗號。爲了解決這個問題,我使用下面的函數:

$array = array(
    "name" => "John", 
    "surname" => "Doe", 
    "email" => "[email protected]" 
); 
function implodeKV($glueKV, $gluePair, $KVarray){ 
    $t = array(); 
    foreach($KVarray as $key=>$val) { 
     $t[] = $key . $glueKV . $val; 
    } 
    return implode($gluePair, $t); 
} 

echo implodeKV(':' , ',' , $array); 
// outputs name:John,surname:Doe,email:[email protected] 

http://phpassist.com/2dde2#2

0

如果你使用PHP 5.3+,然後匿名函數可以使你的代碼會更加清晰,但一個簡單的for循環有最棒的表演。 (使用array_walk接近雖然!)

我跑了一些測試用幾種不同的方法(使用PHP 33年4月5日):

function makeArray(&$a) { 
    $a = array(); 
    for($i = 0; $i < 100000; $i++) { 
     $a[rand()] = rand(); 
    } 
    return $a; 
} 

makeArray($array); 
$before = microtime(true); 
$result = implode(
    ",", 
    array_map(
     function($k, $v) { 
      return "$k:$v"; 
     }, 
     array_keys($array), 
     $array 
    ) 
); 
$after = microtime(true); 
$dur = $after - $before; 
echo "Array Map w/ anonymous function: {$dur}s<br>"; 

makeArray($array); 
$before = microtime(true); 
function kv_func($k, $v) { 
    return "$k:$v"; 
} 
$result = implode(
    ",", 
    array_map(
     "kv_func", 
     array_keys($array), 
     $array 
    ) 
); 
$after = microtime(true); 
$dur = $after - $before; 
echo "Array Map w/ function: {$dur}s<br>"; 

makeArray($array); 
$before = microtime(true); 
array_walk(
    $array, 
    function(&$v, $k) { 
     $v = "$k:$v"; 
    } 
); 
$result = implode(
    ",", 
    $array 
); 
$after = microtime(true); 
$dur = $after - $before; 
echo "Array Walk w/ anonymous function: {$dur}s<br>"; 

makeArray($array); 
$before = microtime(true); 
$ff = true; 
$sep = ","; 
$out = ""; 
foreach($array as $key => $val) { 
    if($ff) $ff = false; 
    else $out .= $sep; 
    $out .= "$key:$val"; 
} 
$after = microtime(true); 
$dur = $after - $before; 
echo "Foreach loop w/ loop flag: {$dur}s<br>"; 

makeArray($array); 
$before = microtime(true); 
$out = ""; 
foreach($array as $key => $val) { 
    $out .= "$key:$val,"; 
} 
$out = substr($out, 0, -1); 
$after = microtime(true); 
$dur = $after - $before; 
echo "Foreach loop + substr: {$dur}s<br>"; 

結果:

Array Map w/ anonymous function: 0.13117909431458s 
Array Map w/ function: 0.13743591308594s // slower than anonymous 
Array Walk w/ anonymous function: 0.065797805786133s // close second 
Foreach loop w/ loop flag: 0.042901992797852s // fastest 
Foreach loop + substr: 0.043946027755737s // comparable to the above 

而只是踢,我嘗試了for循環而不糾正尾隨逗號。它並沒有真正的影響:

Foreach loop w/ trailing comma: 0.044748067855835s