为 PHP 数组的每一项添加前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7617639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:02:38  来源:igfitidea点击:

Add a prefix to each item of a PHP array

phparraysexplodeprefiximplode

提问by MBL

I have a PHP array of numbers, which I would like to prefix with a minus (-). I think through the use of explode and implode it would be possible but my knowledge of php is not possible to actually do it. Any help would be appreciated.

我有一个 PHP 数字数组,我想在它前面加上一个减号 (-)。我认为通过使用爆炸和内爆是可能的,但我对 php 的了解实际上是不可能做到的。任何帮助,将不胜感激。

Essentially I would like to go from this:

基本上我想从这个开始:

$array = [1, 2, 3, 4, 5];

to this:

对此:

$array = [-1, -2, -3, -4, -5];

Any ideas?

有任何想法吗?

回答by Dávid Horváth

An elegant way to prefix array values (PHP 5.3+):

一种为数组值添加前缀的优雅方式(PHP 5.3+):

$prefixed_array = preg_filter('/^/', 'prefix_', $array);

Additionally, this is more than three times faster than a foreach.

此外,这比foreach.

回答by Rohit Chopra

Simple:

简单的:

foreach ($array as &$value) {
   $value *= (-1);
}
unset($value);

Unless the array is a string:

除非数组是字符串:

foreach ($array as &$value) {
    $value = '-' . $value;
}
unset($value);

回答by Peter Ajtai

In this case, Rohit's answeris probably the best, but the PHP array functionscan be very useful in more complex situations.

在这种情况下,Rohit 的答案可能是最好的,但PHP 数组函数在更复杂的情况下可能非常有用。

You can use array_walk()to perform a function on each element of an array altering the existing array. array_map()does almost the same thing, but it returns a new array instead of modifying the existing one, since it looks like you want to keep using the same array, you should use array_walk().

您可以使用array_walk()对数组的每个元素执行一个函数来改变现有数组。array_map()做几乎相同的事情,但它返回一个新数组而不是修改现有数组,因为看起来您想继续使用相同的数组,您应该使用array_walk().

To work directly on the elements of the array with array_walk(), pass the items of the array by reference ( function(&$item)).

要使用 直接处理数组元素,请array_walk()通过引用 ( function(&$item))传递数组项。

Since php 5.3you can use anonymous function in array_walk:

从 php 5.3 开始,你可以在 array_walk 中使用匿名函数:

// PHP 5.3 and beyond!
array_walk($array, function(&$item) { $item *= -1; }); // or $item = '-'.$item;

Working example

工作示例

If php 5.3 is a little too fancy pants for you, just use createfunction():

如果 php 5.3 对你来说有点太花哨了,只需使用createfunction()

// If you don't have PHP 5.3
array_walk($array,create_function('&$it','$it *= -1;')); //or $it = '-'.$it;

Working example

工作示例

回答by JRL

Something like this would do:

像这样的事情会做:

array_map(function($val) { return -$val;} , $array)

回答by SarwarCSE

$array = [1, 2, 3, 4, 5];
$array=explode(",", ("-".implode(",-", $array)));
//now the $array is your required array

回答by Manojkiran.A

I had the same situation before.

我以前也有过同样的情况。

Adding a prefix to each array value

为每个数组值添加前缀

function addPrefixToArray(array $array, string $prefix)
{
    return array_map(function ($arrayValues) use ($prefix) {
        return $prefix . $arrayValues;
    }, $array);
}

Adding a suffix to each array value

为每个数组值添加后缀

function addSuffixToArray(array $array, string $suffix)
{
    return array_map(function ($arrayValues) use ($suffix) {
        return $arrayValues . $suffix;
    }, $array);
}

Now the testing part:

现在测试部分:

$array = [1, 2, 3, 4, 5];

print_r(addPrefixToArray($array, 'prefix'));

print_r(addPrefixToArray($array, 'prefix'));

Result

结果

Array ([0] => prefix1 [1] => prefix2 [2] => prefix3 [3] => prefix4 [4] => prefix5)

print_r(addSuffixToArray($array, 'suffix'));

print_r(addSuffixToArray($array, 'suffix'));

Result

结果

Array ([0] => 1suffix [1] => 2suffix [2] => 3suffix [3] => 4suffix [4] => 5suffix)

回答by Dan Chadwick

You can replace "nothing" with a string. So to prefix an array of strings (not numbers as originally posted):

您可以用字符串替换“无”。因此,要为字符串数组(而不是最初发布的数字)添加前缀:

$prefixed_array = substr_replace($array, 'your prefix here', 0, 0);

That means, for each element of $array, take the (zero-length) string at offset 0, length 0 and replace it the prefix.

这意味着,对于 $array 的每个元素,取偏移量 0 处的(零长度)字符串,长度为 0 并将其替换为前缀。

Reference: substr_replace

参考:substr_replace