PHP:向数组值添加前缀字符串 [英] PHP: Adding prefix strings to array 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"
}
你可以这样做:
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 = array('a', 'b', 'c');
array_walk($array, function(&$value, $key) { $value .= 'd'; } );
这篇关于PHP:向数组值添加前缀字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文