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
2017-02-03, 424👍, 0💬
Popular Posts:
How To Support Multiple-Page Forms in PHP? If you have a long form with a lots of fields, you may wa...
Where to find tutorials on how to work with MySQL Database in PHP? A collection of tutorials to answ...
How To Upload Files into Database in PHP? To store uploaded files to MySQL database, you can use the...
How To Run a PHP Script? A standard alone PHP script can be executed directly with the PHP Command L...
What Is a "dl" Tag/Element? A "dl" element is block level element that can be used to define a defin...