DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Looping or Iterating Over Array Data in PHP
Using loops to iterate over array data is the easiest way to get at all of the data inside an array for evaluating, parsing, or display. When you loop you can access many key value pairs with just a few lines of code. You can conduct any type of operations on the key => value pairs inside of your loops.

This is how we loop over and display data from a simple value based or associative array:
Learn HTML
<?php
$cart_array 
= array("Milk""Cheese""Eggs""Cereal""Jelly"); // has key increment index of 0
// To force key incrementing to start at 1 use this line instead to build your array
//$cart_array = array(1 => "Milk", "Cheese", "Eggs", "Cereal", "Jelly"); // Force key incrementing to start at 1

$i 0// create a varible that will tell us how many items we looped over
foreach ($cart_array as $key => $value) {
$i++; // increment $i by one each loop pass
              
echo "Cart Item $i ______ Key = $key | Value = $value<br />";
}

?>

Develop PHP browser display window
Cart Item 1 ______ Key = 0 | Value = Milk
Cart Item 2 ______ Key = 1 | Value = Cheese
Cart Item 3 ______ Key = 2 | Value = Eggs
Cart Item 4 ______ Key = 3 | Value = Cereal
Cart Item 5 ______ Key = 4 | Value = Jelly



This is how we loop over and display data from a multidimensional array:
Learn HTML
<?php
// create a multidimensional array that holds member details from our site
$members = array (
                  
"member1"  => array ( 
                                       
"name" => "John"
                                       
"zodiac" => "Scorpio"
                                       
"country" => "USA"
                                       
),
                  
"member2"  => array (
                                       
"name" => "Susan"
                                       
"zodiac" => "Virgo"
                                       
"country" => "Ireland"
                                       
),
                  
"member3"  => array (
                                       
"name" => "William",
                                       
"zodiac" => "Pisces"
                                       
"country" => "Great Britain"
                                       
),
                  
"member4"  => array (
                                       
"name" => "Eduardo"
                                       
"zodiac" => "Leo"
                                       
"country" => "Mexico"
                                       
)
                  );

$datasetCount count($members); // returns count of array items of any array
echo "<h1>There are $datasetCount members</h1>";

$i 0;
foreach (
$members as $each_member) {
    
$i++;
    echo 
"<h2>Member $i</h2>";
    while (list(
$key$value) = each ($each_member)) {
                                       
        echo 
"$key: $value<br />";
        
    }

}

?>

Develop PHP browser display window

There are 4 members

Member 1

name: John
zodiac: Scorpio
country: USA

Member 2

name: Susan
zodiac: Virgo
country: Ireland

Member 3

name: William
zodiac: Pisces
country: Great Britain

Member 4

name: Eduardo
zodiac: Leo
country: Mexico



Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2013 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
HTML5