SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

Displaying elements of an array by looping


We can loop through an array to display each element of the array.  This code will display all the element of an array. This is very useful in many occasions where a list is to be displayed. We will see the application of this in check box value collection.

First we will create an array and then display each element of it.

Here is the code to create  and  display the value of the elements of an array.

 $a= array("Three", "two", "Four", "five","ten");

while (list ($key, $val) = each ($a)) {
echo "$key -> $val <br>";
}

Using Print_r

We can use print_r funciton which returns variables in human readable format. Here is the code.

$a= array("Three", "two", "Four", "five","ten");
print_r($a);

The output of the above code is here.

Array ( [0] => Three [1] => two [2] => Four [3] => five [4] => ten )

There are other way like using foreach function to display the elements of an array
Further readings
 PHP array
Array functions in PHP
array : Creating an Array
array_diff Difference of two arrays
array_count_values counting the frequency of values inside an array
count : sizeof Array Size or length
array_push : Adding element to an Array
array_sum : Array Sum of all elements
Displaying the elements of an array
Array checkbox
in_array : IN Array to search for elements inside an array
array_rand : Random elements of Array
array_unique : Array Unique values
Breaking of strings to create array using split command
Session Array to maintain data in different pages
unset : Deleting elements of an array by using its key or value
sort: Simple sorting of PHP array
rsort: Reverse sorting of PHP array
asort: Maintaining index of the elements after sorting
arsort: Sorting in reverse order for a PHP array
ksort: Array key Sorting in PHP
krsort: Array reverse key Sorting in PHP
usort: Sorting array by using user defined function























Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Array Functions
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.