foochuck Posted May 3, 2008 Share Posted May 3, 2008 I have an array I've created with keys and values as so: $myArray["charley"] = "30"; $myArray["chris"] = "28"; $myArray["kate"] = "25"; I was wondering if there's any way to read keys & values from an array like this in a regular for loop somehow? I know how to use a foreach loop, but I want to use a for loop because I need to control how many items from my array appear on the page. PS When I say for loop I mean something like: for ($i = 1; $i <= 10; $i++) { echo $i; } I just don't know the syntax of how to use an array with key / values in a for loop IF it's possible... Thanks FOO Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/ Share on other sites More sharing options...
Gamic Posted May 3, 2008 Share Posted May 3, 2008 <?php foreach($array as $key => $value){ //do stuff } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532393 Share on other sites More sharing options...
sasa Posted May 3, 2008 Share Posted May 3, 2008 <?php $myArray["charley"] = "30"; $myArray["chris"] = "28"; $myArray["kate"] = "25"; $key = array_keys($myArray); for ($i = 0; $i < 3; $i++){ echo $myArray[$key[$i]],"<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532398 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2008 Share Posted May 3, 2008 You might want to consider the array_chunk() function to break the array into pieces the size you want or you can use the each() function in a loop to access the key/value pairs. Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532399 Share on other sites More sharing options...
foochuck Posted May 3, 2008 Author Share Posted May 3, 2008 Gamic: Read closely, I know how to use a foreach loop - but thanks. Sasa: Awesome, that's pretty much what I was looking for. PFMaBismAD: I will look into that option too. Thanks everyone! Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532424 Share on other sites More sharing options...
foochuck Posted May 4, 2008 Author Share Posted May 4, 2008 <?php $myArray["charley"] = "30"; $myArray["chris"] = "28"; $myArray["kate"] = "25"; $key = array_keys($myArray); for ($i = 0; $i < 3; $i++){ echo $myArray[$key[$i]],"<br />\n"; } ?> Quick question - with this method I can access the value, but what about the key? Perhaps I should use a regular array instead of key value pairs for what I'm trying to do...? I have pairs of data I'd like to put in a single array - I need to somehow keep the pairs of data together - while maintaining a limit on the number of pairs of data I'll show on the page from the array. Is there any other way to keep pairs of data together with arrays? FOO Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532550 Share on other sites More sharing options...
hitman6003 Posted May 4, 2008 Share Posted May 4, 2008 use array_keys... $keys = array_keys($data); for ($i = 0; $i < count($keys); $i++) { echo $data[$keys[$i]];; } The example just goes sequentially, but you don't have to. Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532551 Share on other sites More sharing options...
sasa Posted May 4, 2008 Share Posted May 4, 2008 <?php $myArray["charley"] = "30"; $myArray["chris"] = "28"; $myArray["kate"] = "25"; $key = array_keys($myArray); for ($i = 0; $i < 3; $i++){ echo 'key ->',$key[$i],'<br />value ->',$myArray[$key[$i]],"<br />\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532564 Share on other sites More sharing options...
foochuck Posted May 4, 2008 Author Share Posted May 4, 2008 Thanks everyone, my question has been answered! Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-532662 Share on other sites More sharing options...
foochuck Posted May 4, 2008 Author Share Posted May 4, 2008 Hey Guys, I just came up with one more question for you on this subject - I've tried using the sort function (http://www.php.net/sort) on my array and it seems to wipe out the key but retain the value. Is there any way to sort my array by the value's and retain the keys? My value's will have names in them and I want to make sure they are in alphabetical order no matter where they are added in the array. Thanks FOO Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-533012 Share on other sites More sharing options...
PFMaBiSmAd Posted May 4, 2008 Share Posted May 4, 2008 http://www.php.net/manual/en/function.asort.php Quote Link to comment https://forums.phpfreaks.com/topic/103994-array-key-value-in-a-for-loop/#findComment-533049 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.