Example of using array pointer function to manipulate arrays in PHP

  • 2021-08-05 09:11:47
  • OfStack

The internal pointer of an array is the internal organization mechanism of an array, pointing to an element in an array. The default is to point to the first element in the array. You can access any element in the array by moving or changing the position of the pointer. Control of array pointers PHP provides the following built-in functions that can be utilized.

★ current (): Get the content data of the current pointer position.
★ key (): Reads the index value (key value) of the data pointed to by the current pointer.
★ next (): Moves the internal pointer in the array to the next cell.
★ prev (): Rewind the inner pointer of the array by 1 bit.
★ end (): Points the inner pointer of the array to the last 1 element.
★ ES 14EN (): Moves the current pointer unconditionally to the first index position.

These functions have only one parameter, that is, the array itself to be manipulated. In the following example, you will use these array pointer functions to control the order in which elements in an array are read. The code looks like this:


<?php
$contact = array(
"ID" => 1,
" Name " => " Gao Mou ",
" Company " => "A Company ",
" Address " => " Beijing ",
" Telephone " => "(010)98765432",
"EMAIL" => "gao@brophp.com",
);
 
// When an array is first declared, the array pointer is placed in the first 1 Element position
echo ' No. 1 1 Elements: '.key($contact).' => '.current($contact).'<br>'; // No. 1 1 Elements
echo ' No. 1 1 Elements: '.key($contact).' => '.current($contact).'<br>'; // Array pointer not moving
 
next($contact);
next($contact);
echo ' No. 1 3 Elements: '.key($contact).' => '.current($contact).'<br>'; // No. 1 3 Elements
 
end($contact);
echo ' Finally 1 Elements: '.key($contact).' => '.current($contact).'<br>';
 
prev($contact);
echo ' Reciprocal 2 Elements: '.key($contact).' => '.current($contact).'<br>';
 
reset($contact);
echo ' It's back to the first place 1 Elements: '.key($contact).' => '.current($contact).'<br>';
?>

In the previous example, the pointer control functions next (), prev (), end (), and reset () are used to move the pointer position in the array at will, and then the key () and current () functions are used to get the key and value of the current position in the array.


Related articles: