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. Quote 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); ?> Quote 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 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.