About us Privacy Disclaimer Contact us
FAQ Help Advertising Feedback
Home Sitemap Search Donate us

  Home > Computer & I'net > Accessing Arrays...

   Browse by title articles:
   What is hot:

Accessing Arrays

Sorting Arrays

Push and Pop

Exploding and Imploding

Regular Expressions

Metacharacters

Bad Web Design: Strange Cursors
An Intermediate Guide to Formal Visual Design
IFRAMES and their Rising Popularity
Print/Echo
Prev articles12345678 9 101112Next articles



Accessing Arrays


Computer & I'net articlesAccessing Arrays

by David Stanley    



You can access an individual element in an array by calling a specific index value.

<?php
$pantry = array(
   1 => "apples",
   2 => "oranges",
   3 => "bananas"
   );
$findit = $pantry[2];
echo "The value of findit is $findit.";
?>

Gives the result showing :
The value of findit is oranges.

To run through an entire array, a loop would be used with the each command.

<?php
$count_total = count($pantry);
for ($counter=0; $counter<$count_total; $counter++){
$line = each ($pantry);
echo "$line[key] $line[value] <br />";
}
?>

Here's the breakdown :
The total number of elements is calculated.
The FOR loop starts a counter set to zero.
The FOR loop will continue as long as the counter value is less than the number of entries in the array.
The counter is increased by one after each loop is completed.
$line is a temporary array variable. It will hold two values per loop. One value being the KEY or index and the second value being the VALUE of that specific KEY.
The echo command prints out the current values.

And the result is :
1 apples
2 oranges
3 bananas



-----------------
Article by David Stanley. Visit his site http://www.htmlite.com. Reprinted with permission.





Interest compare


  Disclaimer | Privacy | Terms of useCopyright © 2004 Nice2know.com