How to make the first value of an array as a key for the second value as a value in a php array

I have arrays structured as shown below:

array(2) { ["uid"]=> string(2) "39" ["name"]=> string(18) "Manoj Kumar Sharma" } array(2) { ["uid"]=> string(2) "47" ["name"]=> string(11) "S kK Mishra" } 

I want this array to be as follows:

 array(4) { [39]=> string(18) "Manoj Kumar Sharma" [47]=> string(11) "S kK Mishra" } 

How can i achieve this? Please help me.

+7
arrays php
source share
1 answer

Updated

You can try this with array_column () -

 $new = array_column($arr, 'name', 'uid'); 

Demo

Note: array_column() not available for PHP <5.5

If you are using lower versions of PHP , use a loop.

 $new = array(); foreach($your_array as $array) { $new[$array['uid']] = $array['name']; } 
+7
source share

All Articles