Tools, FAQ, Tutorials:
Looping through an Array in PHP
How to Loop through an Array in PHP?
✍: FYIcenter.com
The best way to loop through an array is to use the "foreach" statement. There are two forms of "foreach" statements:
Here is a PHP script on how to use "foreach" on an array:
<?php
$array = array("Zero"=>"PHP", "One"=>"Perl", "Two"=>"Java");
$array["3"] = "C+";
$array[""] = "Basic";
$array[] = "Pascal";
$array[] = "FORTRAN";
print("Loop on value only:\n");
foreach ($array as $value) {
print("$value, ");
}
print("\n\n");
print("Loop on key and value:\n");
foreach ($array as $key=>$value) {
print("[$key] => $value\n");
}
?>
This script will print:
Loop on value only: PHP, Perl, Java, C+, Basic, Pascal, FORTRAN, Loop on key and value: [Zero] => PHP [One] => Perl [Two] => Java [3] => C+ [] => Basic [4] => Pascal [5] => FORTRAN
⇒ Order of Array Values in PHP
2017-02-03, ∼2228🔥, 0💬
Popular Posts:
How to use the "set-body" Policy Statement for an Azure API service operation? The "set-body" Policy...
How to use "link" command tool to link objet files? If you have object files previously compiled by ...
How To Get the Minimum or Maximum Value of an Array in PHP? If you want to get the minimum or maximu...
How To Avoid the Undefined Index Error in PHP? If you don't want your PHP page to give out errors as...
What are "*..." and "**..." Wildcard Parameters in Function Definitions? If you want to define a fun...