使用PHP中的键与数组内爆

时间:2012-11-28 12:03:57

标签: php arrays csv

我需要在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

我将如何实现这一目标?

5 个答案:

答案 0 :(得分:10)

试试这段代码:

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

答案 1 :(得分: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;

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分:-1)

<?php
$array = array(
"name" => "John",
"surname" => "Doe",
"email" => "nowhere@nowhere.com"
);
foreach( $array as $key=>$data ) {
$output .= $comma . $key . ':' . $data;
$comma = ',';
}

echo $output;

?>