Sunday 4 April 2021

Php: Array pointers

Php maintains an internal pointer that points to one of the item in the array. By default, this pointer points to the first element in the array. Php provides functions to move the pointer in forward, reverse, to the end, to the first position.

 

Following table summarizes the functions.

 

Function

Description

current ( array|object $array ) : mixed

Return current element of the array. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns false.

next ( array|object &$array ) : mixed

It advances the internal array pointer one place forward before returning the element value.

prev ( array|object &$array ) : mixed

it rewinds the internal array pointer one place instead of advancing it.

reset ( array|object &$array ) : mixed

rewinds array's internal pointer to the first element and returns the value of the first array element.

end ( array|object &$array ) : mixed

advances array's internal pointer to the last element, and returns its value.

 

Let’s see it with an example.

 

array_pointers_demo.php

#!/usr/bin/php

<?php

$arr1 = array(2, 3, 5, 7, 11);

echo "Elements in array are : \n";
print_r($arr1);

$current_element = current($arr1);

echo "\nCurrent element : $current_element\n";

echo "\nAdvancing internal pointer to next index\n";
$current_element = next($arr1);

echo "\nElement returned by next function: $current_element\n";

$current_element = current($arr1);
echo "Current element : $current_element\n";

echo "\nAdvancing current pointer to one step backward\n";
$current_element = prev($arr1);
echo "\nElement returned by prev function : $current_element\n";

$current_element = current($arr1);
echo "\nCurrent element : $current_element\n";

echo "\nMoving the internal pointer to the end\n";
$current_element = end($arr1);
echo "\nElement returned by end function : $current_element\n";

$current_element = current($arr1);
echo "\nCurrent element : $current_element\n";

?>

 

Output

$./array_pointers_demo.php 

Elements in array are : 
Array
(
    [0] => 2
    [1] => 3
    [2] => 5
    [3] => 7
    [4] => 11
)

Current element : 2

Advancing internal pointer to next index

Element returned by next function: 3
Current element : 3

Advancing current pointer to one step backward

Element returned by prev function : 2

Current element : 2

Moving the internal pointer to the end

Element returned by end function : 11

Current element : 11

 

Using array pointers to traverse the array

Traversing in forward direction

while($current_ele = current($arr1)){

    echo "current element : $current_ele\n";

    next($arr1);

}

 

Traversing in backward direction

while($current_ele = current($arr1)){

    echo "current element : $current_ele\n";

    prev($arr1);

}

 

Find the below working application.

 

array_traversal_with_pointers.php

 

#!/usr/bin/php

<?php

$arr1 = array(2, 3, 5, 7, 11);

echo "Traversing in forward direction\n";
while($current_ele = current($arr1)){
    echo "current element : $current_ele\n";
    next($arr1);
}

echo "\n\nTraversing in backward direction\n";
end($arr1);
while($current_ele = current($arr1)){
    echo "current element : $current_ele\n";
    prev($arr1);
}

?>

 

Output

$./array_traversal_with_pointers.php 

Traversing in forward direction
current element : 2
current element : 3
current element : 5
current element : 7
current element : 11


Traversing in backward direction
current element : 11
current element : 7
current element : 5
current element : 3
current element : 2

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment