ayok Posted February 21, 2011 Share Posted February 21, 2011 Hi, Here is simple question. I have this array: array([0]=>name [1]=>Tony); How can i change into array(name=>Tony); ? Thank you, ayok Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/ Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 $a = array([0]=>name [1]=>Tony); $b = array($a[0]=>$a[1]); Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177610 Share on other sites More sharing options...
ayok Posted February 21, 2011 Author Share Posted February 21, 2011 Oh..... yes of course.. Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177611 Share on other sites More sharing options...
ayok Posted February 21, 2011 Author Share Posted February 21, 2011 One more question. I have this array: Array ( 0 => Array ( [width] => 500 ), 1 => Array ( [height] => 300 ), 2 => Array ( [depth] => 500) ) How can I make to: Array([width] => 500 [height] => 300) [depth] => 500) ? I can do of course with array_merge(array[0],array[1],array[2]) or array[0] + array[1] + array[2], but the amount of arrays would be different. I got error if there are only 2 arrays. What could be the trick? Any help would be appreciated. Ayok Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177653 Share on other sites More sharing options...
Jessica Posted February 21, 2011 Share Posted February 21, 2011 Use foreach. Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177654 Share on other sites More sharing options...
ayok Posted February 21, 2011 Author Share Posted February 21, 2011 I've tried with foreach, but maybe I've used it wrong, so I always get the same result. no change. Could you show me the right way? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177662 Share on other sites More sharing options...
Psycho Posted February 21, 2011 Share Posted February 21, 2011 There are several ways you could achieve this. Here is one function reKeyArray($inputArray) { if(!is_array($inputArray)) { return false; } $outputArray = array(); foreach($inputArray as $subArray) { if(!is_array($subArray)) { continue; } $outputArray = array_merge($outputArray, $subArray); } return $outputArray; } Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177665 Share on other sites More sharing options...
ayok Posted February 21, 2011 Author Share Posted February 21, 2011 Goh! Thank you guru! I got headache from this.. Quote Link to comment https://forums.phpfreaks.com/topic/228384-create-array-key-from-value/#findComment-1177681 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.