PHP阵列 - 添加字符串的所有值 [英] PHP Arrays - Adding a String to all Values
本文介绍了PHP阵列 - 添加字符串的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是添加特定或多个值到一个数组的最佳方式?
有点难以解释,但这应该帮助:
What is the best way to add a specific value or values to an array? Kinda hard to explain, but this should help:
<?php
$myarray = array("test", "test2", "test3");
$myarray = array_addstuff($myarray, " ");
var_dump($myarray);
?>
它输出:
array(3) {
[0]=>
string(5) " test"
[1]=>
string(6) " test2"
[2]=>
string(6) " test3"
}
您可以这样做是这样的:
You could do so like this:
function array_addstuff($a, $i) {
foreach ($a as &$e)
$e = $i . $e;
return $a;
}
但我不知道是否有一个更快的方法,如果此功能是内置。
But I'm wondering if there's a faster way, or if this function is built-in.
推荐答案
在这种情况下,你使用的是PHP版本> = 5.3:
In the case that you're using a PHP version >= 5.3:
$array = ('a', 'b', 'c');
array_walk($array, function(&$value, $key) { $value .= 'd'; });
这篇关于PHP阵列 - 添加字符串的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文