Home php How to add prefix in each key of PHP Array?

How to add prefix in each key of PHP Array?

by Shahriar Sagor

Today now in this tutorial i will show you how to add prefix in each key of php array. When we are require to add one character or more to each and every key of array then we most of the people would like prefer add key by using for loop or by using foreach loop. But if we want then we can do this without using any kind of loop in our application. we can do this by using array_combine(), array_keys() and array_map() through we can add prefix on each key of an array, son now in the bellow example we will see how to use both function together and how to add faster way to add prefix of every item key of array:

$myArray = ['0'=>'Hi','1'=>'Hello','2'=>'Hey'];
$myNewArray = array_combine(
    array_map(function($key){ return 'a'.$key; }, array_keys($myArray)),


    [a0] => Hi
    [a1] => Hello
    [a2] => Hey

Read Also : How to push item to 0 index or first of $scope object in AngularJS?

Thanks for read. I hope it help you. For more you can follow us on facebook.


You may also like