downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pos> <natsort
Last updated: Fri, 24 Jul 2009

view this page in

next

(PHP 4, PHP 5)

next배열의 내부 배열 포인터를 전진

설명

mixed next ( array &$array )

next()current()와 비슷하게 동작하지만 한가지 차이를 갖고 있다. 원소값을 반환하기 전에 내부 배열 포인터를 한칸 다음으로 전진시킨다. 이 말은 다음 배열 값을 반환하고 내부 배열 포인터를 한칸 전진시킨다는 뜻이다.

인수

array

영향 받는 array

반환값

내부 배열 포인터가 가리키는 다음 위치의 배열 값을 반환하거나, 더 이상 원소가 없을 경우에는 FALSE를 반환합니다.

Warning

이 함수는 논리 FALSE를 반환하지만, 0이나 "" 등, 논리 FALSE로 취급할 수 있는 다른 값을 반환할 수 있습니다. 자세한 정보는 논리형 섹션을 참고하십시오. 이 함수의 반환값을 확인하려면 === 연산자를 이용하십시오.

예제

Example #1 next()의 용례와 관련 함수

<?php
$transport 
= array('foot''bike''car''plane');
$mode current($transport); // $mode = 'foot';
$mode next($transport);    // $mode = 'bike';
$mode next($transport);    // $mode = 'car';
$mode prev($transport);    // $mode = 'bike';
$mode end($transport);     // $mode = 'plane';
?>

주의

Note: 배열의 끝과 boolean FALSE 원소를 구별할 수 없습니다. FALSE 원소를 가지는 배열을 올바르게 따라가려면, each()를 참고하십시오.

  • current() - 배열의 현재 원소를 반환
  • end() - 배열 내부 포인터가 마지막 원소를 가리키게 설정
  • prev() - 내부 배열 포인터를 후진
  • reset() - 배열의 내부 포인터를 첫 원소로 설정
  • each() - 배열에서 현재 키와 값 쌍을 반환하고 배열 커서를 전진



pos> <natsort
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
next
ThinkMedical at Gmail dot com
02-Sep-2008 10:27
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] = '...';
}
adityabhai [at] gmail com [Aditya Bhatt]
09-May-2008 12:14
<?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);
    
    }
   
    public function
getPrev () {
    
     
self::setCurrent($this->curr);
      return
prev($this->all);
    
    }
      
  }
?>

Demo Example:

<?php
   $steps
= new Steps();
  
$steps->add('1');
  
$steps->add('2');
  
$steps->add('3');
  
$steps->add('4');
  
$steps->add('5');
  
$steps->add('6');
  
$steps->setCurrent('4');
   echo
$steps->getCurrent()."<br />";
   echo
$steps->getNext()."<br />";
   echo
$steps->getPrev()."<br />";
  
$steps->setCurrent('2');
   echo
$steps->getCurrent()."<br />";
   echo
$steps->getNext()."<br />";
   echo
$steps->getPrev()."<br />";
?>
darkside at i dot ua
11-Dec-2007 11:36
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 />";
?>
gg2005 at gmail dot com
06-Feb-2007 09:32
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....
}
GPatmore
12-Oct-2006 07:14
If your using a foreach loop, unless you for a reference, PHP will make a copy of the array to use it the loop.

So, when I need to take a different action for the last element in the array I use the following:

<?php
$ary 
= explode(',','a,b,c,d,e,f,g');
foreach(
$ary as $a){
    print
'letter ' . $a;
    if(
next($ary)){
        print
'<br>';
    }else{
        print
'<br>dun!';
    }
}
?>

Output:

letter a
letter b
letter c
letter d
letter e
letter f
letter g
dun!

NOTE::
PHP5 has added an ability to reference the variable in a foreach like:

<?php
foreach($ary as &$a){}
?>

this will probably cause undesired results when using the method above.

also if the array is changed in any way as to cause the length of the original array to become different then the copy, it will not work. 
Consider the following example:

<?php
$ary 
= explode(',','a,b,c,d,e,f,g');
foreach(
$ary as $a){
    print
'letter ' . $a;
    if(
next($ary)){
        unset(
$ary[count($ary) - 1]);
        print
'<br>';
    }else{
        print
'<br>dun!';
    }
}
?>

output:

letter a
letter b
letter c
letter d
dun!letter e
dun!letter f
dun!letter g
dun!
tom at nono dot be
26-Jul-2006 11:19
I see some questions like "how can I know if an array has a next value without changing its internal pointer" and some pretty complicated responses that DO work mind you and in some cases you'll need them...

But suppose you just need a different action within a foreach loop when reaching the final item:
(it won't be usefull in every situation, but in most it will)

$numOfItems = count($someArray);
$counter = 0;
foreach ($someArray as $key => value){
    $counter += 1;
    if ($counter <> $numOfItems){
        //here all next items exist
   } else {
        //final item
   }
}
Sigmar
30-May-2006 10:02
And if you want to know if there are any array elements in array before given key, you can use this function:

function any_array_keys_before($keyname, &$array) {
    if (!is_array($array)) {
        return false;
    }
    if (empty($keyname)) {
        return false;
    }
    $set = false;
    $count = 0;
    foreach ($array as $key => $value) {
        if ($set === false && $key != $keyname) {
            $count ++;
        } else if ($key == $keyname) {
            $set = true;
        }
    }
    return $count > 0 ? true : false;
}
Sigmar
30-May-2006 09:52
If you want to check, if there are some more elements in array after given key, you can use the following function:

function more_array_keys($keyname, &$array) {
    if (!is_array($array)) {
        return false;
    }
    if (empty($keyname)) {
        return false;
    }
    $set = false;
    $count = 0;
    foreach ($array as $key => $value) {
        if ($set === true) {
            $count++;
        }
        if ($key == $keyname) {
            $set = true;
        }
    }
    return $count > 0 ? true : false;
}
tino at infeon dot com
13-May-2006 03:48
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
brentimus
28-Apr-2005 05:10
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);
    }
}
?>
papipo's gmail account
13-Oct-2004 08:47
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.
lukasz at karapuda dot com
18-Aug-2004 05:06
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;
}
?>
court shrock
20-May-2004 11:36
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;
}

?>
bm at ANTISPAM dot solidwave dot com
17-Apr-2004 04:49
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

pos> <natsort
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites