How to set value as key in PHP array?
Sometimes, we work on big php or other framework projects and we need to change array value as array key at that time you can learn from this post. In this example you can do that without foreach loop. For example if you have all country array but that array have key like 1,2,3,4... etc and value is country. at that time you need to make array like country as key and value both that way you can store it on database table directly.
We can do that using array_combine() of PHP array. array_combine() take two argument and both should be array. so, let's see exmple.
Example:
$myArray = ['1'=>'US','2'=>'India','3'=>'Brazil','4'=>'Germany'];
$result = array_combine($myArray,$myArray);
print_r($result);
Output:
Array
(
[US] => US
[India] => India
[Brazil] => Brazil
[Germany] => Germany
)

Hardik Savani
I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.