Traversing through Arrays in php

  /  
Traversing Through Array in php

Arrays are also variables but are able to hold any number of values and also can remember those values. On the other hand a simple variable can hold one value at a time. In php arrays can be numerically indexed or associative.

A simplest way of creating an array is as under:

$cart = array('item1', 'item2', 'item3');

this $cart is an array that hold 3 items. For displaying these items we will write as: echo $cart[0];, echo $cart[1];, echo $cart[2];.

Yes, numerical keys are automatically assigned when you declare the array as above. and these numerical index starts from 0.

Second Type of arrays are of associative type and these are created like this:

$cart = array(

'item1'=> 'soap'

'item2' => 'oil'

'item3' => 'facewash'

);

Here we are assigning keys and value pairs. so for assessing any value we will be using these keys instead of numerical keys as under

 echo $cart['item1']

result would be

soap

After this basic introduction let us move to our main motto, i.e. how to traverse through arrays.

The requirement of traversing through arrays and retrieving various values or keys or both is common among programmers. So if you are a programming working in php, these handy snippets should be in your toolbox.

Retrieving the current array key:

The key(); function returns the key located at the current pointer position of the given array
The following example will output the $cart_items array keys by iterating over the array and moving the pointer:

$cart_items = array(

'item1'=> 'soap'

'item2' => 'oil'

'item3' => 'facewash'

);
while($key = key($cart_items)) {
 printf("%s
", $key);
 next($cart_items);
}

The result will be as under:

item1

item2

item3

Retrieving the current array Value:

There is a php inbuilt function named current(); that will output the current values as the pointer moves through the array

$cart_items = array(

'item1'=> 'soap'

'item2' => 'oil'

'item3' => 'facewash'

);
while($value = current($cart_items)) {
 printf("%s
", $value);
 next($cart_items);
}

The result will be as under:

soap

oil

facewash

Retrieving the Current Array Key and Value:

The php function foreach(); does a great job in retrieving these keys and value pairs. The application part goes like this:

$cart_items = array(

'item1'=> 'soap'

'item2' => 'oil'

'item3' => 'facewash'

);
foreach($cart_items as $kay => $value) {
 echo $key. '='.$value.'<br/>';
}

And output will be as under:

item1 = soap
item2 = oil
item3 = facewash

Moving the pointer to next array Position:

The php next();function is available that moves the pointer to the next position, as under:

$fruits = array("apple", "orange", "banana");

$fruit = next ($fruits);      // returns 'orange'

$fruit = next ($fruits);      // returns 'banana'

Moving to the Pointer to previous Position:

Similar to the next(); function, the prev(); function moves the pointer to previous position. Try it yourself.

Moving the Pointer to the First Array Position:

php inbuilt function reset(); does the job of moving the array pointer to first position. for eg.

$fruits = array("apple", "orange", "banana");

$fruit = next ($fruits);      // returns 'orange'

$fruit = next ($fruits);      // returns 'banana'
$fruit = reset($fruits);      // returns 'apple'

Moving the Pointer to the last Array Position:

The php function end(); is available to move the poinet to last array position. Left for Brevity.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>