jump to navigation

walking through an array September 17, 2008

Posted by Nsh15 in php, PHP Arrays, Variables and Data.
Tags: , , , , , , , , ,
trackback

will often want to do sth to all the elements in the array. So u need to walk through every element in an array, in order is called Iteration or traversing.

There is two way to traversing an array:

  • traversing an array manually (use an array pointer to move from one value to another value)
  • using foreach (Automatically walks through the array from beginning to end, one value at a time)

Traversing an array manually:

This can be done using a pointer, so lets think about the array as a list and the pointer stays in its place till u move it the next value, and there it will stay till u move it again and so on…

Pointer instructions:

  • current($array_name): Refers to the value currently under the pointer, it doesn’t move the pointer.
  • next($array_name): Moves the pointer to the value after the current value.
  • previous($array_name): Moves the pointer to the value before the current value.
  • end($array_name): moves the pointer to the last value in the array.
  • reset($array_name): moves the pointer to the first value in the array.

Example:

<?php

$pointer_array= array (“A1″,”B1″,”C1″,”A2″,”B2″,”C2″,”A3″,”B3″,”C3″);

$pointer= reset($pointer_array);
echo “The value in the first element “, $pointer,”<br>”;

$pointer= next($pointer_array);
echo “The value in the second element “, $pointer,”<br>”;

$pointer= next($pointer_array);
$pointer= next($pointer_array);
echo “The value in the fourth element “, $pointer,”<br>”;

$pointer= end($pointer_array);
echo “The value in the last element “, $pointer,”<br>”;

?>
The output:

The value in the first element A1
The value in the second element B1
The value in the fourth element A2
The value in the last element C3

Using foreach to walk through an array:

u can use foreach to walk through an array one element at a time and execute a block of statements on ‘em.

foreach( $array_name as $key_name => $value_name )

{

Block of statements

}

where:

  • $array_name: The name of the array u want to walk through it
  • $key_name: The name of the variable where u want to store the key (Optional)
  • $value_name: The name of the variable where u want to store the value

U don’t need to reset the pointer when u start using foreach statement coz it starts from the beginning of the array.

Example:

<?php

$foreach_array= array (“A1″,”B1″,”C1″,”A2″,”B2″,”C2″);

foreach ($foreach_array as $foreach_key => $foreach_variable)
{ echo “The “, $foreach_key, ” element is “, $foreach_variable, “<br>”;

};

?>

The output:

The 0 element is A1
The 1 element is B1
The 2 element is C1
The 3 element is A2
The 4 element is B2
The 5 element is C2

Comments»

1. Outdoor Activities - September 17, 2008

[...] me2blog placed an interesting blog post on walking through an arrayHere’s a brief overview$array_name: The name of the array u want to walk through it; $key_name: The name of the variable where u want to store the key (Optional); $value_name: The name of the variable where u want to store the value … [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.