Add a prefix to each item of a PHP array

PhpArraysExplodePrefixImplode

Php Problem Overview


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.

Essentially I would like to go from this:

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

to this:

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

Any ideas?

Php Solutions


Solution 1 - Php

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

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

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

Solution 2 - Php

Simple:

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

Unless the array is a string:

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

Solution 3 - Php

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

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().

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

Since php 5.3 you can use 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 a little too fancy 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

Solution 4 - Php

Something like this would do:

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

Solution 5 - Php

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.

Reference: substr_replace

Solution 6 - Php

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

Solution 7 - Php

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'));

###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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMBLView Question on Stackoverflow
Solution 1 - PhpDávid HorváthView Answer on Stackoverflow
Solution 2 - PhpRohit ChopraView Answer on Stackoverflow
Solution 3 - PhpPeter AjtaiView Answer on Stackoverflow
Solution 4 - PhpJRLView Answer on Stackoverflow
Solution 5 - PhpDan ChadwickView Answer on Stackoverflow
Solution 6 - PhpSarwar HasanView Answer on Stackoverflow
Solution 7 - PhpManojKiran AppathuraiView Answer on Stackoverflow