maxim Posted July 31, 2006 Share Posted July 31, 2006 Hi i have a accoiative array. it holds 10 elements. i loop through it like so[code="php"] foreach ($array as $key =>$val) { print "$key - $val";}[/code]this prints out the 10 values.i would like to only prind out lets say 3.how would i go about doing this. i tryed putting the foreach loop in a while loop. but it either prints out the 10 element array 3 times. or it prints out the first element 3 times. Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/ Share on other sites More sharing options...
king arthur Posted July 31, 2006 Share Posted July 31, 2006 [code]for($i = 0; $i < 3; $i++){list($key, $value) = each($array);echo "$key - $value";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-66249 Share on other sites More sharing options...
maxim Posted July 31, 2006 Author Share Posted July 31, 2006 hay that worked. thanks - ive never used the list(); function before because i was under the impression that foreach was the best way to loop through arrays.thanks again this should help me in the future Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-66254 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 And if you want to use foreach and the array keys are from 0 to 9, then you can just use[code]foreach ($array as $key =>$val) { print "$key - $val"; if($key == 2) break;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-66301 Share on other sites More sharing options...
maxim Posted July 31, 2006 Author Share Posted July 31, 2006 ahh yes i how ever had a accoiative array. with diffrent strings as the keys Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-66312 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 You can also use this:[code]foreach ($array as $key =>$val) { print "$key - $val"; if($i == 3) break; $i++;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-66314 Share on other sites More sharing options...
maxim Posted August 1, 2006 Author Share Posted August 1, 2006 hah thats a very smart way to do it. its easyer to read and understand also so ill use it. i perfer foreach loops also. how ever theres a small but. its actualy loops through the array four time not three. be cause $i starts off as 0. so. you need to assign a value of 1 to $i, out side the loop.[code]$i = 1;foreach ($array as $key =>$val) { print "$key - $val<br />"; if($i == 3) break; $i++;}[/code]of course you could just use 2 insted of three. like you did in your original post.[code]if($i == 2) break;[/code]but i set the value of $i as the code is more logicalbut yeah thanks heaps for that Ifa. the break; keyword is definalty one to remember Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-67179 Share on other sites More sharing options...
gerkintrigg Posted August 1, 2006 Share Posted August 1, 2006 I'd have done it Ifa's way too... Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-67198 Share on other sites More sharing options...
Ifa Posted August 1, 2006 Share Posted August 1, 2006 Yeap, your right about the $i thingy... One can't remeber it all :) Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-67293 Share on other sites More sharing options...
maxim Posted August 16, 2006 Author Share Posted August 16, 2006 hay ifa or any one else. i was reading up on the python prgramming language and long story short its a strange language! it calls arrays "lists" any way they have a inbuilt kinda thing called sliceing. to only output a certian number of elemnts in a list (or array).i checked out the php manual and sure enough php has a array_slice() fuction. take a lookhttp://au2.php.net/function.array-sliceso with out testing it i assume this will produce the same results[code]$first_three = array_slice($array, 0, 3);[/code]what do you guys think of the array_slice() fuction ? is it better to use it ? or just stick with looping through a bigger array ans using break; to stop the loop once you have your deired output ?edit::actualy i dont think it works on assoc arrays :( Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-75799 Share on other sites More sharing options...
Barand Posted August 16, 2006 Share Posted August 16, 2006 Works fine on associative arrays, but the count/break is more efficient.[code]<?php$array = array('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5,'f'=>6,'g'=>7,'h'=>8,'i'=>9);$count=0;foreach ($array as $key =>$val) { print "$key - $val<br/>"; if (++$count == 3) break;}echo "--------------------------------<br/>";$subarray = array_slice($array, 0, 3);foreach ($subarray as $key =>$val) { print "$key - $val<br/>";}?>[/code]-->[pre]a - 1b - 2c - 3--------------------------------a - 1b - 2c - 3[/pre] Quote Link to comment https://forums.phpfreaks.com/topic/16078-looping-through-a-array/#findComment-75945 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.