next

(PHP 4, PHP 5)

nextDahili dizi göstericisini bir ilerletir

Açıklama

mixed next ( array &$dizi )

next() işlevi bir farkla current() işlevi gibi davranır. Eleman değerini döndürmeden önce dahili dizi göstericisini bir ileri taşır. Yani farklı olarak dahili dizi göstericisini bir ilerlettikten sonra, current() işlevindeki gibi dahili dizi göstericisinin gösterdiği elemanın değerini döndürür.

Değiştirgeler

dizi

Göstericisi konumlanacak dizi.

Dönen Değerler

Dizinin dahili göstericisinin bulunduğu konumun bir sonrasında eleman varsa o elemanın değeri döner, yoksa FALSE döner.

Uyarı

Bu işlev mantıksal FALSE değeriyle dönebileceği gibi FALSE olarak değerlendirilebilecek mantıksal olmayan bir değerle de dönebilir. Bu konuda daha fazla bilgi edinmek için Mantıksal Değerler bölümüne bakabilirsiniz. Bu işlevden dönen değeri sınamak için === işlecini kullanınız.

Örnekler

Örnek 1 - next() ve şürekası

<?php
$araç 
= array('tabanvay''bisiklet''otomobil''uçak');
$kip current($araç); // $kip = 'tabanvay';
$kip next($araç);    // $kip = 'bisiklet';
$kip next($araç);    // $kip = 'otomobil';
$kip prev($araç);    // $kip = 'bisiklet';
$kip end($araç);     // $kip = 'uçak';
?>

Notlar

Bilginize: Bir dizinin sonundaki elemanın değerinin boolean FALSE olup olmadığını anlayamazsınız. FALSE değerli elemanlar içeren bir dizide bilinçli olarak ilerlemenin en doğru yolu each() kullanmaktır.

Ayrıca Bakınız

  • current() - Bir diziden gösterici konumundaki elemanı döndürür
  • end() - Bir dizinin dahili göstericisini sonuncu elemana konumlandırır
  • prev() - Dahili dizi göstericisini bir geriletir
  • reset() - Bir dizinin dahili göstericisini ilk elemana konumlar
  • each() - Bir diziden, gösterici konumundaki anahtar değer çiftini döndürdükten sonra göstericiyi bir ilerletir

add a note add a note

User Contributed Notes 10 notes

up
7
papipo's gmail account
9 years ago
I need to know if an array has more items, but without moving array's internail pointer. Thats is, a has_next() function:

<?php
function has_next($array) {
    if (
is_array($array)) {
        if (
next($array) === false) {
            return
false;
        } else {
            return
true;
        }
    } else {
        return
false;
    }
}

$array = array('fruit', 'melon');
if (
has_next($array)) {
    echo
next($array);
}

// prints 'melon'
?>

Since you do not pass the array by reference, its pointer is only moved inside the function.
Hope that helps.
up
5
brentimus
9 years ago
Papipo's function below is usefull in concept but does not work.

"Since you do not pass the array by reference, its pointer is only moved inside the function."

This is true, but the array you are manipulating in your has_next() function will have it's pointer set to the first element, not the same position as the original array. What you want to do is pass the array to the has_next() function via reference. While in the has_next() function, make a copy of the array to work on. Find out the current pointer position of the original array and set the pointer on the working copy of the array to the same element. Then you may test to see if the array has a "next" element.

Try the followig insetad:

<?php
function has_next(&$array)
{
   
$A_work=$array//$A_work is a copy of $array but with its internal pointer set to the first element.
   
$PTR=current($array);
   
array_set_pointer($A_work, $PTR);

    if(
is_array($A_work))
    {
        if(
next($A_work)===false)
            return
false;
        else
            return
true;
    }
    else
        return
false;
}

function
array_set_pointer(&$array, $value)
{
   
reset($array);
    while(
$val=current($array))
    {
        if(
$val==$value)
            break;

       
next($array);
    }
}
?>
up
1
court shrock
10 years ago
This code returns neighbors of the specified key.  The result will be empty if it doesn't have any neighbors.  My approach was to use the order of keys to determine neighbors, which is differnet from just getting the next/previous element in an array.  Feel free to point out stupidities :)

<?php

function array_neighbor($arr, $key)
{
   
krsort($arr);
   
$keys = array_keys($arr);
   
$keyIndexes = array_flip($keys);
   
   
$return = array();
    if (isset(
$keys[$keyIndexes[$key]-1]))
       
$return[] = $keys[$keyIndexes[$key]-1];
    if (isset(
$keys[$keyIndexes[$key]+1]))
       
$return[] = $keys[$keyIndexes[$key]+1];

    return
$return;
}

?>
up
1
ThinkMedical at Gmail dot com
5 years ago
regarding references with foreach, you can use them directly. Obviating various posts which provide many lines of 'work arounds'.

$array = array(1,2,3,4,5);

foreach($array as &$value)

or use $key

foreach($array as $key => $value)
{
    $array[$key] = '...';
}
up
1
darkside at i dot ua
6 years ago
This class implements simple operations with array

<?php
class Steps {
   
    private
$all;
    private
$count;
    private
$curr;
   
    public function
__construct () {
     
     
$this->count = 0;
     
    }
   
    public function
add ($step) {
     
     
$this->count++;
     
$this->all[$this->count] = $step;
     
    }
   
    public function
setCurrent ($step) {
     
     
reset($this->all);
      for (
$i=1; $i<=$this->count; $i++) {
        if (
$this->all[$i]==$step) break;
       
next($this->all);
      }
     
$this->curr = current($this->all);
     
    }
   
    public function
getCurrent () {
     
      return
$this->curr;
     
    }
   
    public function
getNext () {
     
     
self::setCurrent($this->curr);
      return
next($this->all);
     
    }
       
  }
?>

usage example:

<?php
   $steps
= new Steps();
  
$steps->add('one');
  
$steps->add('two');
  
$steps->add('three');
  
$steps->setCurrent('one');
   echo
$steps->getCurrent()."<br />";
   echo
$steps->getNext()."<br />";
  
$steps->setCurrent('two');
   echo
$steps->getCurrent()."<br />";
   echo
$steps->getNext()."<br />";
?>
up
1
bm at ANTISPAM dot solidwave dot com
10 years ago
Take care when replacing code using reset()/next() with code using foreach as foreach does not update the array's internal pointer.  This means you cannot, say, use next() to skip an element in foreach loop, or use current() within a function to get a reference to the current element.  You probably have code depending on this internal pointer and replacing it will be more work than you anticipated.

See http://www.php.net/foreach
up
0
gg2005 at gmail dot com
7 years ago
Don't confuse next with continue!

If you're a Perl developer starting with PHP, you might try to use "next" inside a loop to skip to the next iteration...  

i.e.,

foreach ($things as $thing) {
  if (something I don't like about $thing) {
   next;
  }
  blah....
}

The php compiler will take next... but it's not going to work.

Do this instead:
foreach ($things as $thing) {
  if (something I don't like about $thing) {
   continue;
  }
  blah....
}
up
0
tino at infeon dot com
8 years ago
this may be handy and i didnt know where else to post it.. i need a simple function to cycle through an array i eventually made it into a class so i could have multiple cycles.. if you like it or find it usefull please email me and let me know

class Cycle
{
    var $position;
    var $dataArray;
    var $dataArrayCount;
   
    function Cycle()
    {
        $this->dataArray = func_get_args();
        $this->dataArrayCount = count($this->dataArray);
    }
   
    function Display()
    {
        $this->position = (!isset($this->position) || $this->position >= ($this->dataArrayCount - 1)) ? 0 : $this->position += 1;
        return $this->dataArray[$this->position];
    }
   
}

$bgColor = new Cycle('#000000', '#FFFFFF', '#FF0000');

echo $bgcolor->Display();
//returns #000000
echo $bgcolor->Display();
//returns #FFFFFF
echo $bgcolor->Display();
//returns #FF0000
echo $bgcolor->Display();
//returns #000000
up
0
lukasz at karapuda dot com
9 years ago
This function will return the previous,next neighbors of an array entry within an associative array. If the specified $key points to the last or first element of the array, the first or last keys of the array will be returned consecutively. This is an improved version of the same function posted earlier.

<?php
function array_neighbor($arr, $key)
{
  
$keys = array_keys($arr);
  
$keyIndexes = array_flip($keys);
 
  
$return = array();
   if (isset(
$keys[$keyIndexes[$key]-1])) {
      
$return[] = $keys[$keyIndexes[$key]-1];
   }
   else {
      
$return[] = $keys[sizeof($keys)-1];
   }
  
   if (isset(
$keys[$keyIndexes[$key]+1])) {
      
$return[] = $keys[$keyIndexes[$key]+1];
   }
   else {
      
$return[] = $keys[0];
   }
  
   return
$return;
}
?>
up
-1
double at dumpit dot de
4 years ago
PHP: 5.2.10-2ubuntu6.3 (default apt-get installation on actual, see Date, jaunty 9.10 Ubuntu Distro - G33kWoRDs)

Have a look at your array pointer if you copy an array - the pointer will be copied, too.

For example if you got this construct:
<?php
    $array
= array('zero','one','two','three','four','five','six','seven');
   
$array2 = $array;
   
next($array);
    echo
key($array);
    echo
key($array2);

   
// will output:
    // 1
    // 0
?>

But if you copy the array after you've setted the pointer, the pointer will be copied, too:
<?php
    $array
= array('zero','one','two','three','four','five','six','seven');
   
next($array);
   
$array2 = $array;
    echo
key($array);
    echo
key($array2);
  
   
// will output:
    // 1
    // 1
?>

What's more is, that foreach not resetting the pointer after walk through:
<?php

    $array
= array('zero','one','two','three','four','five','six','seven');
   
next($array);
   
$array2 = array();
    foreach(
$array AS $key => $value){
        echo
$key;
       
$array2[$key] = $value;
    }
    echo
var_dump(key($array));
    echo
key($array2);

   
// will output for foreach:
    // 0 1 2 3 4 5 6 7
    // and for the keys
    // NULL
    // 0
?>

The php-functions seems to reset the pointer on the given position after walk through (i don't know the internal handling - there could be used a copy of the array, too):
<?php

    $array
= array('zero','one','two','three','four','five','six','seven');
   
next($array);
   
$array2 = array_values($array);
    echo
key($array);
    echo
key($array2);

   
// will output:
    // 1
    // 0
?>

There are a lot Methods like array_merge($array) that will neither reset the pointer of $array nor copy the pointer to $array2. Have a look on this.
I Hope this was a little helpfull.
To Top