Jump to content


Photo

looping through a array


  • Please log in to reply
10 replies to this topic

#1 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 31 July 2006 - 07:37 AM

Hi i have a accoiative array. it holds 10 elements. i loop through it like so

foreach ($array as $key =>$val) {
           print "$key - $val";
}

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.

#2 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 31 July 2006 - 07:49 AM

for($i = 0; $i < 3; $i++)
{
list($key, $value) = each($array);
echo "$key - $value";
}

Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#3 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 31 July 2006 - 08:08 AM

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

#4 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 12:09 PM

And if you want to use foreach and the array keys are from 0 to 9, then you can just use
foreach ($array as $key =>$val) {
           print "$key - $val";
           if($key == 2) break;
}

I'm just a guy who likes to code for fun...

#5 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 31 July 2006 - 12:22 PM

ahh yes i how ever had a accoiative array. with diffrent strings as the keys

#6 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 12:28 PM

You can also use this:
foreach ($array as $key =>$val) {
           print "$key - $val";
           if($i == 3) break;
           $i++;
}

I'm just a guy who likes to code for fun...

#7 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 01 August 2006 - 05:28 PM

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.

$i = 1;
foreach ($array as $key =>$val) {
           print "$key - $val<br  />";
           if($i == 3) break;
           $i++;
}
of course you could just use 2 insted of three. like you did in your original post.
if($i == 2) break;

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

#8 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 01 August 2006 - 05:58 PM

I'd have done it Ifa's way too...
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#9 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 01 August 2006 - 08:04 PM

Yeap, your right about the $i thingy... One can't remeber it all :)
I'm just a guy who likes to code for fun...

#10 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 16 August 2006 - 06:11 PM

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/f...ion.array-slice

so with out testing it i assume this will produce the same results

$first_three = array_slice($array, 0, 3);

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

#11 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 August 2006 - 11:04 PM

Works fine on associative arrays, but the count/break is more efficient.
<?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/>";
}

?>

-->[pre]
a - 1
b - 2
c - 3
--------------------------------
a - 1
b - 2
c - 3

[/pre]
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users