physaux Posted February 2, 2010 Share Posted February 2, 2010 Here is the kind of array I have, sorted by the 'score': $players['george'] = 81; $players['simon'] = 74; $players['sally'] = 44; ... Now I am in the middle of an array, And I want to print out the 'name' with the score: foreach($players as $player){ echo ???NAME???.' -- '.$player.'</br>'; } I want it to output like so george -- 81 simon -- 74 sally -- 44 So could anyone please tell me how I can get the values? I know there is some function that will make me a new array with the index values, but I can't remember it... $namearray = mysteryfunction($players); and $namearray would be $namearray[0] = "george"; $namearray[1] = "simon"; $namearray[2] = "sally"; ... Quote Link to comment https://forums.phpfreaks.com/topic/190691-how-can-i-get-the-index-values-of-an-array-into-a-new-array/ Share on other sites More sharing options...
wildteen88 Posted February 2, 2010 Share Posted February 2, 2010 Change your foreach to $players['george'] = 81; $players['simon'] = 74; $players['sally'] = 44; foreach($players as $key => $player){ echo $key.' -- '.$player.'</br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/190691-how-can-i-get-the-index-values-of-an-array-into-a-new-array/#findComment-1005656 Share on other sites More sharing options...
physaux Posted February 2, 2010 Author Share Posted February 2, 2010 Ah thanks so much that worked great. I'm surprised, I have never used foreach loops like that! Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/190691-how-can-i-get-the-index-values-of-an-array-into-a-new-array/#findComment-1005660 Share on other sites More sharing options...
wildteen88 Posted February 2, 2010 Share Posted February 2, 2010 Yeah foreach does have two formats. As explained in the PHP Manual. Quote Link to comment https://forums.phpfreaks.com/topic/190691-how-can-i-get-the-index-values-of-an-array-into-a-new-array/#findComment-1005661 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.