Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/16078-looping-through-a-array/
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
  • 2 weeks later...
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 :(
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.