Add a prefix to each element of the PHP array

I have an array of PHP numbers that I would like a prefix with minus (-). I think using bang and bang would be possible, but my php knowledge really can't do it. Any help would be appreciated.

Essentially, I would like to move from this:

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

:

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

Any ideas?

+63
arrays php prefix implode explode
01 Oct '11 at 1:50
source share
8 answers

Simply:

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

If the array is not a string:

 foreach ($array as &$value) { $value = '-' . $value; } unset($value); 
+92
01 Oct 2018-11-11T00:
source share

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

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

In addition, it is more than three times faster than foreach .

+111
Jan 23 '15 at 17:51
source share

In this case, Rohit's answer is probably the best, but the functions of the PHP array can be very useful in more complex situations.

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

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

Since php 5.3 you can use an anonymous function in array_walk:

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

Working example

If php 5.3 is too unusual pants for you, just use createfunction() :

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

Working example

+65
01 Oct '11 at 4:00
source share

Something like that:

 array_map(function($val) { return -$val;} , $array) 
+20
01 Oct 2018-11-11T00:
source share
 $array = "1, 2, 3, 4"; $array = explode( ',', $array ); foreach ($array as &$value){ $value = '-' . trim($value); } $array = implode(', ', $array); //output "-1, -2, -3, -4" 
+4
Aug 21 '13 at 1:11
source share
 $array = [1, 2, 3, 4, 5]; $array=explode(",", ("-".implode(",-", $array))); //now the $array is your required array 
+3
Sep 26 '16 at 8:29
source share

You can replace โ€œnothingโ€ with a string. So that the prefix is โ€‹โ€‹an array of strings (and not numbers as originally sent):

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

This means that for each element of $ the_array take a string (zero length) with offset 0, length 0 and replace it with a prefix.

Link: substr_replace

+1
Apr 25 '19 at 18:59
source share

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 part of the testing:

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

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

Result

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

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

Result

 Array ([0] => 1suffix [1] => 2suffix [2] => 3suffix [3] => 4suffix [4] => 5suffix) 
0
Apr 17 '19 at 8:07
source share



All Articles