currentRETURNS the current element in an ARRAY.
This function is an alias of
pos.
pos
RETURNS the current element in an ARRAY.
This function is an alias of current.
This function returns the value of the array element that's currently being pointed to by the internal pointer.
This function does not move the pointer in any way.
If the internal pointer points beyond the end of the elements list or the array is empty the returned value is FALSE.
To test the returned value the operator === must be used.
This function may return also a non-Boolean value which is evaluated as FALSE.
<?php
mix current ( arr &$array )
or
mix pos ( arr &$array )
where,
$array = The input ARRAY
?>
$array
The given input array.
Can be passed by reference.
EXERCISE
<?php
$arr01 = array("countries" => array("Brasil", "Chile"),
"continent" => "South America");
echo 'The given ARRAY:<br>';
print_r($arr01);
echo '<br><br><br>';
echo 'The current INDEX:<br>';
$ndx01ca = key($arr01);
print_r($ndx01ca);
echo '<br><br>The current INDEX value:<br>';
$ele01ca = current($arr01);
print_r($ele01ca);
?>
RESULT
The given ARRAY:
Array
(
[countries] => Array
(
[0] => Brasil
[1] => Chile
)
[continent] => South America
)
The current INDEX:
countries
The current INDEX value:
Array
(
[0] => Brasil
[1] => Chile
)
EXERCISE
<?php
$arr02 = [];
echo 'The given ARRAY:<br>';
print_r($arr02);
echo '<br><br><br>';
echo 'The current INDEX:<br>';
$ndx02ca = key($arr02);
var_dump($ndx02ca);
echo '<br><br>The current INDEX value:<br>';
$ele02ca = current($arr02);
var_dump($ele02ca);
?>
RESULT
The given ARRAY:
Array ()
The current INDEX:
NULL
The current INDEX value:
bool(false)
EXERCISE
<?php
$arr03 = [ -8, 0, 1, 2, 3, "5" ];
echo 'The given ARRAY:<br>';
print_r($arr03);
echo '<br><br><br>';
echo 'The current INDEX: ';
$ndx03ca = key($arr03);
print_r($ndx03ca);
echo '<br><br>The current INDEX value: ';
$ele03ca = current($arr03);
print_r($ele03ca);
?>
RESULT
The given ARRAY:
Array ( [0] => -8 [1] => 0 [2] => 1 [3] => 2 [4] => 3 [5] => 5 )
The current INDEX: 0
The current INDEX value: -8
EXERCISE
<?php
echo 'The given ARRAY:<br>';
$arr04d = array( -8, 0, 1, 2, 3, "5" );
print_r($arr04d);
echo '<br><br>Current INDEX: ';
$ndx04dc = key($arr04d);
print_r($ndx04dc);
echo '<br><br>Current ELEMENT: ';
$ele04dc = current($arr04d);
print_r($ele04dc);
echo '<br><br><br>Removing the FIRST ELEMENT.<br>Resulting ARRAY:<br>';
array_shift($arr04d);
print_r($arr04d);
echo '<br><br>Current INDEX: ';
$ndx04de = key($arr04d);
print_r($ndx04de);
echo '<br><br>Current ELEMENT: ';
$ele04de = current($arr04d);
print_r($ele04de);
?>
RESULT
The given ARRAY:
Array
(
[0] => -8
[1] => 0
[2] => 1
[3] => 2
[4] => 3
[5] => 5
)
Current INDEX: 0
Current ELEMENT: -8
Removing the FIRST ELEMENT.
Resulting ARRAY:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 5
)
Current INDEX: 0
Current ELEMENT: 0
EXERCISE
<?php
echo 'The given ARRAY:<br>';
$arr05d = array( -8, 0, 1, 2, 3, "5" );
print_r($arr05d);
echo '<br><br>Current INDEX: ';
$ndx05dc = key($arr05d);
print_r($ndx05dc);
echo '<br><br>Current ELEMENT: ';
$ele05dc = pos($arr05d);
print_r($ele05dc);
echo '<br><br><br>Removing the FIRST ELEMENT.<br>Resulting ARRAY:<br>';
array_shift($arr05d);
print_r($arr05d);
echo '<br><br>Current INDEX: ';
$ndx05de = key($arr05d);
print_r($ndx05de);
echo '<br><br>Current ELEMENT: ';
$ele05de = pos($arr05d);
print_r($ele05de);
?>
RESULT
The same results as the previous exercise!