Chose Language:
Author: Admin/Publisher |finished | checked

PHP next function

The PHP next() function moves the internal pointer to the next position in the array and returns the value of the array element at that position.

PHP next function
Description / Descripción
next(array &$array): mixed
Sintaxis / Sintax
$nextvalue = next($array);
For arrays that may have false values, it is recommended to use foreach, as next cannot distinguish the end properly.

Similarly, you can use next but you must take the precaution of checking that the key is null. To do this, you can use the PHP key function.

Parameters

$array – the array whose pointer will be changed to the next position.

Returns

mixed (can be any value) – the value of the next element

false – in case the array has no more elements

PHP next examples

First of all, let’s do a simple example with any array.

Ejemplo simple de next
$autos = array("Toyota", "Ford", "Chevrolet", "Nissan", "Honda");

$nextvalue= next($autos);
echo $nextvalue;

The next function can also be used to iterate through an array using while, although for this purpose I would recommend foreach. Let’s see an example that we saw before:

Este es un ejemplo de php.net
<?php
$array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
    if ($fruit_name == 'apple') {
        echo key($array), "\n";
    }
    next($array);
}
?>
fruit1
fruit4
fruit5

This is an excellent example for you to see how we can iterate with while, but keep in mind what we said before, remember that if our array contains false this can cause us problems.

Let’s see one more example about this: if we have the following array, how would we iterate through it using while and next?

array
$persona = array(
    "nombre" => "Juan",
    "edad" => 25,
    "estudiante" => false,
    "ocupacion" => "Carpintero"
);

To iterate through this array, we should do something like this:

Ejemplo de next cuando existe un valor false
$persona = array(
    "nombre" => "Juan",
    "edad" => 25,
    "estudiante" => false,
    "ocupacion" => "Carpintero"
);

while (true) {
    $valor = current($persona);
    $clave = key($persona);
    
    if ($clave === null) {
        break;
    }

    echo "Clave: $clave, Valor: $valor\n";

    // Mover el puntero al siguiente elemento
    next($persona);
}
Clave: nombre, Valor: Juan
Clave: edad, Valor: 25
Clave: estudiante, Valor: 
Clave: ocupacion, Valor: Carpintero

Notice that we have done a while true, this will make this loop execute continuously unless $key is null, this is obtained using the PHP key function.

If $key is null we will call a break which will make us exit this loop.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category