SeanHarding Posted November 22, 2011 Share Posted November 22, 2011 I need some help converting my captured array values into keys and later add arrays as the values. This might help explain what I'm looking for: I currently have Array ( [0] => Meat [1] => Veg ) But I'm after Array ( Meat => Veg => ) My end result will be: [Veg] => Array ( [Potatoe] => 12 [Cabbage] => 24 [Cucumber] => 56 ) [Meat] => Array ( [Chicken] => 23 [beef] => 90 ) I think either array_values() may help but I'm not too sure how to use it. Link to comment https://forums.phpfreaks.com/topic/251611-change-array-values-to-keys/ Share on other sites More sharing options...
cyberRobot Posted November 22, 2011 Share Posted November 22, 2011 For the first part of your quetions, you could use a foreach() loop to create the associative array. For example: <?php //INITIALIZE VARIABLES $originalArray = array('meat', 'veggies', 'candy'); $newArray = array(); //CREATE THE ASSOCIATIVE ARRAY foreach($originalArray as $currItem) { $newArray[$currItem] = ''; } //SHOW VARIABLES var_dump($originalArray) . '<br />'; var_dump($newArray); ?> Link to comment https://forums.phpfreaks.com/topic/251611-change-array-values-to-keys/#findComment-1290390 Share on other sites More sharing options...
PFMaBiSmAd Posted November 22, 2011 Share Posted November 22, 2011 array_flip Link to comment https://forums.phpfreaks.com/topic/251611-change-array-values-to-keys/#findComment-1290391 Share on other sites More sharing options...
SeanHarding Posted November 22, 2011 Author Share Posted November 22, 2011 array_flip Perfect thank you Link to comment https://forums.phpfreaks.com/topic/251611-change-array-values-to-keys/#findComment-1290393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.