array_column() 返回input数组中键值为column_key的列, 如果指定了可选参数index_key,那么input数组中的这一列的值将作为返回数组中对应值的键。 参数 input 需要取出数组列的多维数组(或结果集) column_key 需要返回值的列,它可以是索引数组的列索引,或者是关联数组的列的键。 也可以是NULL,此时将返回整个数组(配合index_key参数来重置数组键的时候,非常管用)…
April 15, 2018
PHP: 数组成员批量添加前缀, Adding prefix strings to array values
问题:
$myarray = array("test", "test2", "test3"); #转换为 $myarray = array("prefix_test", "prefix_test2", "prefix_test3");
方法一:
$myarray = array("test", "test2", "test3"); array_walk($myarray, function(&$value, $key) { $value = 'prefix_'.$value; } );
方法二:
$myarray = array("test", "test2", "test3"); $array = array_map(function($value) { return ' prefix_'.$value; }, $myarray);
方法三:
$myarray = array("test", "test2", "test3"); $prefixed_array = preg_filter('/^/', 'prefix_', $myarray);
本文:PHP: 数组成员批量添加前缀, Adding prefix strings to array values