51

If I know the length of an array, how do I print each of its values in a loop?

1
  • 1
    You don't need to book-keep the length of the array to iterate over it Commented Jan 28, 2013 at 13:55

12 Answers 12

129
$array = array("Jonathan","Sampson");

foreach($array as $value) {
  print $value;
}

or

$length = count($array);
for ($i = 0; $i < $length; $i++) {
  print $array[$i];
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note that the for() loop doesn't work on arrays with string indexes (obviously)
Count should be precalculated. In your example, it's being calculated on every loop. It should be: for ($i = 0, $count = count($array); $i < $count; $i++)
14

Use a foreach loop, it loops through all the key=>value pairs:

 foreach($array as $key=>$value){
     print "$key holds $value\n";
 }

Or to answer your question completely:

 foreach($array as $value){
     print $value."\n";
 }

Comments

11

for using both things variables value and kye

foreach($array as $key=>$value){
 print "$key holds $value\n";
}

for using variables value only

foreach($array as $value){
 print $value."\n";
}

if you want to do something repeatedly until equal the length of array us this

// for loop
for($i = 0; $i < count($array); $i++) {
 // do something with $array[$i]
}

Thanks!

Comments

5

Here is example:

$array = array("Jon","Smith");
foreach($array as $value) {
  echo $value;
}

1 Comment

Could you please elaborate more your answer adding a little more description about the solution you provide?
4

I also find that using <pre></pre> tags around your var_dump or print_r results in a much more readable dump.

Comments

2
foreach($array as $key => $value) echo $key, ' => ', $value;

Comments

2

either foreach:

foreach($array as $key => $value) {
  // do something with $key and $value
}

or with for:

for($i = 0, $l = count($array); $i < $l; ++$i) {
  // do something with $array[$i]
}

obviously you can only access the keys when using a foreach loop.

if you want to print the array (keys and) values just for debugging use var_dump or print_r

Comments

1
while(@$i++<count($a))
echo $a[$i-1];

3v4l.org

Comments

1

Another advanced method is called an ArrayIterator. It’s part of a wider class that exposes many accessible variables and functions. You are more likely to see this as part of PHP classes and heavily object-oriented projects.

$fnames = ["Muhammed", "Ali", "Fatimah", "Hasan", "Hussein"];

$arrObject = new ArrayObject($fnames);
$arrayIterator = $arrObject->getIterator();

while( $arrayIterator->valid() ){
echo $arrayIterator->current() . "<br />";
$arrayIterator->next();
}

Comments

0

If you're debugging something and just want to see what's in there for your the print_f function formats the output nicely.

Comments

0

Additionally, if you are debugging as Tom mentioned, you can use var_dump to see the array.

Comments

0

Foreach before foreach: :)

reset($array); 
while(list($key,$value) = each($array))
{
  // we used this back in php3 :)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.