How to set value as key in PHP array?
In this post we will give you information about How to set value as key in PHP array?. Hear we will give you detail about How to set value as key in PHP array?And how to use it also give you demo for it if it is necessary.
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
)
Hope this code and post will helped you for implement How to set value as key in 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