[php] PHP array printing using a loop

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

This question is related to php arrays loops

The answer is


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


Here is example:

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

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!


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();
}

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


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


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

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

or

$length = count($array);
for ($i = 0; $i < $length; $i++) {
  print $array[$i];
}

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";
 }

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

Foreach before foreach: :)

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

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

3v4l.org


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


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true