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 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]); 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... 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 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. 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. 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; } 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.. 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
Archived
This topic is now archived and is closed to further replies.