0

我需要在 PHP 中将数组写入 .csv 文件。

示例数组:

$array = array(
    "name" => "John",
    "surname" => "Doe",
    "email" => "nowhere@nowhere.com"
);

通过使用implode(",", $array),我得到这样的结果:
John,Doe,nowhere@nowhere.com

但是,我还需要将每个元素的键写入文件。

所需的输出是这样的:
name:John,surname:Doe,email:nowhere@nowhere.com

我将如何实现这一目标?

4

5 回答 5

11

试试这个代码:

$out = $sep = '';
foreach( $array as $key => $value ) {
    $out .= $sep . $key . ':' . $value;
    $sep = ',';
}
于 2012-11-28T12:06:43.657 回答
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;
于 2012-11-28T12:06:41.730 回答
0

上面的答案在末尾输出一个逗号。为了纠正这个问题,我使用以下函数:

$array = array(
    "name" => "John",
    "surname" => "Doe",
    "email" => "nowhere@nowhere.com"
);
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:nowhere@nowhere.com

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

于 2014-08-01T22:52:34.353 回答
0

如果您使用的是 PHP 5.3+,那么匿名函数可以使您的代码更简洁,尽管简单的 for 循环具有最佳性能。(array_walk虽然使用接近!)

我用几种不同的方法(使用 PHP 5.4.33)运行了一些测试:

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
于 2014-11-17T17:53:59.610 回答
-1
<?php
$array = array(
"name" => "John",
"surname" => "Doe",
"email" => "nowhere@nowhere.com"
);
foreach( $array as $key=>$data ) {
$output .= $comma . $key . ':' . $data;
$comma = ',';
}

echo $output;

?>
于 2012-11-28T12:31:30.103 回答