The main PHP built-in function used to iterate through array elements is
foreach()
. We learned about the
foreach() loop in a dedicated tutorial.
Here is an example of how to loop through an array using foreach.
php<?php
$file_list = array(
"index" => "home_page.php",
"article" => "article_page.php",
"about" => "about_page.php",
"contact" => "contact_page.php"
);
/*
* Loop through the array and print all the values
*/
if(is_array($file_list)){
foreach($file_list as $file){ // iterate files
echo $file . "<br />";
}
}
?>
This loop should print something like this:
phphome_page.php
article_page.php
about_page.php
contact_page.php
Using foreach()
like this will ignore the key name. But if you want to also get the key you must add it to the equation like this:
php<?php
if(is_array($file_list)){
foreach($file_list as $key=>$file){ // iterate files
echo "'$file' is stored on key '$key'<br />";
}
}
?>
This loop will give you the following result:
php'home_page.php' is stored on key 'index'
'article_page.php' is stored on key 'article'
'about_page.php' is stored on key 'about'
'contact_page.php' is stored on key 'contact'
You can also get all array values using the
for()
function. To do that you will need to previously get the total number of elements, that your array has. This is called the length of an array and we use
count()
to get it.
Imagine we have this following array. Here is how we get the length of it:
php<?php
$templates = array( "home", "article", "about", "contact" );
$length = count($templates);
echo "The array has $length elements."; // The array has 4 elements.
?>
let's now continue and iterate throw all the array values using
for()
function
php<?php
$templates = array( "home", "article", "about", "contact" );
$length = count($templates);
// build all file-names system using the $templates array
for($i=0; $i < $length; $i++) {
echo $templates[$i] ."_page.php<br />";
}
?>
This will return the following result
phphome_page.php
article_page.php
about_page.php
contact_page.php
Till now we have created our own array values, so you always know the values that we are working with. How about receiving an array from a function, database or another file and you have no idea what your array contains.
Let's apply the
print_r()
on our first defined array. We used the
pre
tag to maintain the formatting.
php<?php
echo "<pre>";
print_r($file_list);
echo "</pre>";
?>
This is how we obtain all the array elements. This is just beautiful ;-)
phpArray
(
[index] => home_page.php
[article] => article_page.php
[about] => about_page.php
[contact] => contact_page.php
)
Another function that will display an array like this is
var_dump()
. var_dump will display structured information about the array that includes the type and value. Arrays are explored recursively with values indented to show structure. Depending on your needs you can use
var_dump()
or
print_r()
.
phpvar_dump($file_list);
Here is the output:
phparray(4) { ["index"]=> string(13) "home_page.php" ["article"]=> string(16) "article_page.php" ["about"]=> string(14) "about_page.php" ["contact"]=> string(16) "contact_page.php" }
You can always apply the pre tag to it to add some formatting
php<?php
echo "<pre>";
var_dump($file_list);
echo "</pre>";
?>
Here is the output:
phparray(4) {
["index"]=>
string(13) "home_page.php"
["article"]=>
string(16) "article_page.php"
["about"]=>
string(14) "about_page.php"
["contact"]=>
string(16) "contact_page.php"
}