Jump to content

Archived

This topic is now archived and is closed to further replies.

maxim

looping through a array

Recommended Posts

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.

Share this post


Link to post
Share on other sites
[code]
for($i = 0; $i < 3; $i++)
{
list($key, $value) = each($array);
echo "$key - $value";
}
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
ahh yes i how ever had a accoiative array. with diffrent strings as the keys

Share this post


Link to post
Share on other sites
You can also use this:
[code]foreach ($array as $key =>$val) {
          print "$key - $val";
          if($i == 3) break;
          $i++;
}[/code]

Share this post


Link to post
Share on other sites
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 logical

but yeah thanks heaps for that Ifa. the break; keyword is definalty one to remember

Share this post


Link to post
Share on other sites
Yeap, your right about the $i thingy... One can't remeber it all :)

Share this post


Link to post
Share on other sites
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 look

http://au2.php.net/function.array-slice

so 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 :(

Share this post


Link to post
Share on other sites
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 - 1
b - 2
c - 3
--------------------------------
a - 1
b - 2
c - 3

[/pre]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.