ItSolutionStuff.com

How to Add Prefix in Each Key of PHP Array?

By Hardik Savani • May 14, 2024
PHP

Hey Dev,

In this short tutorial, we will cover a array add prefix on keys php. I explained simply step by step php array add prefix to keys. I explained simply about php array add prefix. We will use php add prefix to each element in array.

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

)

I hope it can help you...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Curl POST Request with Headers Example

Read Now →

How to Connect SSH using PHP?

Read Now →

Laravel PHP json_decode without quotes Example

Read Now →

How to Check If a String Contains HTML Tags in PHP?

Read Now →

PHP - Dropzone Add Download Button Example

Read Now →

PHP Multidimensional Array Search By Value Example

Read Now →

PHP Check If Array Is Multidimensional or Not

Read Now →

PHP Signature Pad Example | E-Signature Pad using Jquery Ajax and PHP

Read Now →

PHP MySQL Contact US Form with Validation Example

Read Now →

How to Make Read More Link from String in PHP?

Read Now →

PHP - Getting Started PHPUnit Test Example

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →