How to add prefix in each key of PHP Array?

How to add prefix in each key of PHP Array?

In this post we will give you information about How to add prefix in each key of PHP Array?. Hear we will give you detail about How to add prefix in each key of PHP Array?And how to use it also give you demo for it if it is necessary.

Whenever you require to add one character or more to each and every key of array then most of the people would like prefer add key using for loop or foreach loop. But we can do this without using any kind of loop. using array_combine(), array_keys() and array_map() through we can add prefix on each key of array, In Bellow example you can see how to use both function together and add faster way to add prefix of every item key:

Example:

$myArray = ['0'=>'Hi','1'=>'Hello','2'=>'Hey'];

$myNewArray = array_combine(

array_map(function($key){ return 'a'.$key; }, array_keys($myArray)),

$myArray

);

print_r($myNewArray);

Output:

Array

(

[a0] => Hi

[a1] => Hello

[a2] => Hey

)

Hope this code and post will helped you for implement How to add prefix in each key of PHP Array?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  62  =  64

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US