doctor_james Posted September 19, 2007 Share Posted September 19, 2007 Hi guys . suppose we have an array like this : $MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9); how can I generate a1,a2,a3... keys in a "For" loop instead of printing all the elements seprately , I'm looking for some method like this ( I know about "foreach" but I wanna use this method for some reasons ) : for ($i=0;$i<5;$i++) { echo $MyArray[...]; // How can I get the key (...) ? } instead of : echo $MyArray['a1']; echo $MyArray['a12]; echo $MyArray['a3']; . . . thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/69880-array-question/ Share on other sites More sharing options...
hemlata Posted September 19, 2007 Share Posted September 19, 2007 Hello doctor_james, Use foreach loop to get the array keys. $MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9); foreach($MyArray as $key => $value) { // $key will be a1, a2,... and corresponing values will be 5,6,.... } Hope this helps you. Regards Quote Link to comment https://forums.phpfreaks.com/topic/69880-array-question/#findComment-351005 Share on other sites More sharing options...
doctor_james Posted September 19, 2007 Author Share Posted September 19, 2007 thanks man . as I said I know about foreach loop but I wanna generate the keys in a for loop . for some reason I can't use foreach. Quote Link to comment https://forums.phpfreaks.com/topic/69880-array-question/#findComment-351008 Share on other sites More sharing options...
wildteen88 Posted September 19, 2007 Share Posted September 19, 2007 You cannot catch the key from an array in a for loop, unless you know what the keys are in the array. If the keys are defined as a1, a2 ... a12 etc. Then you could do: $MyArray = array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9); for ($i = 1; $i < count($MyArray); $i++) { $key = 'a' . $i; echo $key . ' = ' . $MyArray[$key] . '<br />'; } However foreach would be better as its sole purpose is to loop through arrays. $MyArray=array("a1"=>5,"a2"=>6,"a3"=>7,"a4"=>8,"a5"=>9); foreach($MyArray as $key => $value) { echo $key . ' = ' . $value . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/69880-array-question/#findComment-351301 Share on other sites More sharing options...
hemlata Posted September 24, 2007 Share Posted September 24, 2007 Hello, You cannot catch the key from an array in a for loop, You can get the array keys in forloop using following code // loop for array count for ($i=0; $i<count($MyArray); $i++) { // get the current key and associated value for test array list($var, $val) = each($MyArray); // diplay key-value for array echo 'Key :: ' . $var . ' value:: ' . $val .'<br>'; } Hope this could solve your problem. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/69880-array-question/#findComment-353885 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.