|
|
|
|
| PHP current |
 |
|
|
| ±Û¾´ÀÌ : ¼Õ´Ô
³¯Â¥ : 05-12-20 15:01
Á¶È¸ : 1771
|
|
http://cafe.naver.com/q69/9325 (293) | |
|
|
|
current
(PHP 3, PHP 4 , PHP 5) current -- ¹è¿ÀÇ ÇöÀç ¿ø¼Ò¸¦ ¹ÝȯÇÑ´Ù
¼³¸ímixed current ( array array)
¸ðµç ¹è¿Àº "ÇöÀç" ¿ø¼Ò¸¦ °¡¸®Å°´Â ³»ºÎ Æ÷ÀÎÅ͸¦ °®´Âµ¥, ÀÌ ³»ºÎ Æ÷ÀÎÅÍ´Â ¹è¿¿¡ »ðÀԵǾîÁø ù¹øÂ° ¿ø¼Ò·Î ÃʱâȵȴÙ.
current() ÇÔ¼ö´Â ´Ü¼øÈ÷ ÇöÀç ³»ºÎ Æ÷ÀÎÅÍ¿¡¼ °¡¸®Å°°í ÀÖ´Â ¹è¿ ¿ø¼ÒÀÇ °ªÀ» ¹ÝȯÇÑ´Ù. ¾î¶² ¹æ¹ýÀ¸·Îµç ±× Æ÷ÀÎÅ͸¦ ¿òÁ÷ÀÌÁö ¾Ê´Â´Ù. ³»ºÎ Æ÷ÀÎÅͰ¡ ¿ø¼Ò ¸ñ·ÏÀÇ ³¡¿¡¼ ¹þ¾î³ °÷À» °¡¸®Å°¸é, current()´Â FALSE¸¦ ¹ÝȯÇÑ´Ù.
| ÁÖÀÇ |
|
¹è¿ÀÌ ºó ¿ø¼Ò¸¦ Æ÷ÇÔÇϸé(0 À̳ª "", ºó ¹®ÀÚ¿) ÀÌ ÇÔ¼ö´Â ÀÌ ¿ø¼Òµé¿¡ ´ëÇØ¼µµ FALSE¸¦ ¹ÝȯÇÑ´Ù. current()¸¦ »ç¿ëÇÏ¿© ±× ¹è¿ ¾ÈÀÇ ¸ñ·Ï ³¡ÀÎÁö È®ÀÎÇÒ¼ö ¾ø°Ô ¸¸µç´Ù. ºó ¿ø¼Ò¸¦ Æ÷ÇÔÇÒ¼öµµ ÀÖ´Â ¹è¿À» ÀûÀýÇÏ°Ô °Å´Ò±â À§Çؼ´Â, each() ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ´Ù. |
¿¹ 1. current()ÀÇ »ç¿ë¿¹¿Í °ü·Ã ÇÔ¼ö
|
<?php $transport = array('foot', 'bike', 'car', 'plane'); $mode = current($transport); $mode = next($transport); $mode = current($transport); $mode = prev($transport); $mode = end($transport); $mode = current($transport); ?>
| |
end(), key(), next(), prev(), reset() Âü°í.
toreriks at hotmail dot com05-Aug-2005 01:18
By doing some small changes to the last post, one can make the function seek not just from the start in a forward manner but also from the end or from a relative position both forwards and backwards. E.g to seek backwards from a relative position one would supply 'current' as $start, and 'prev' as $iter.
function array_set_current(&$array, $key, $start='reset', $iter='next'){ $start($array); while (current($array)!==FALSE){ if (key($array) == $key) { break; } $iter($array); } return current($array); }
jari dot eskelinen at iki dot fi02-Dec-2004 01:08
mattblisset's array_set_current has one flaw: if you have array with key 0, while-loop will stop there. Fixed version:
function array_set_current(&$array, $key){ reset($array); while (current($array)!==FALSE){ if (key($array) == $key) { break; } next($array); } return current($array); }
mdeng at kabenresearch dot com24-Apr-2004 06:04
For large array(my sample was 80000+ elements), if you want to traverse the array in sequence, using array index $a[$i] could be very inefficient(very slow). I had to switch to use current($a).
mattblissett at hotmail dot com01-Jan-2004 10:36
Sam said: "Here is a function that sets the current pointer of an array to a given key."
If your array pointer doesn't start at the beginning, this won't work for moving the pointer to a lower one. So, use a reset() first:
<?php function array_set_current(&$array, $key){ reset($array); while(current($array)){ if(key($array) == $key){ break; } next($array); } } ?>
HTH, Matt
vitalib at 012 dot net dot il02-Dec-2003 10:10
Note that by copying an array its internal pointer is lost:
<?php $myarray = array(0=>'a', 1=>'b', 2=>'c'); next($myarray); print_r(current($myarray)); echo '<br>'; $a = $myarray; print_r(current($a)); ?>
Would output 'b' and then 'a' since the internal pointer wasn't copied. You can cope with that problem using references instead, like that:
<?php $a =& $myarray; ?>
jdpipe at yahoo dot co dot uk24-Nov-2003 05:08
You can't get a pointer to an element in an array using the 'current' construct, for example:
<?php
$a=array();
$a[]='aaa'; $a[]='bbb'; $a[]='ccc'; $a[]='ddd'; $a[]='eee';
reset($a); next($a); $v =& current($a); $v = 'xxxx';
print_r($a);
next($a);
$v =& $a[key($a)]; $v = 'yyy';
print_r($a);
?>
si15-Sep-2003 05:57
current isn't able to return by reference, eg.
function &getDataPacket() { global $globalref; return current( $globalref[ key( $globalref ) ]->dp ); // *FAILS* return $globalref[ key( $globalref ) ]->dp[ key( $globalref[ key( $globalref ) ]->dp ) ]; // *WORKS* }
According to Bug#12692 it's intentional, so watch out!
tipman08-May-2003 11:07
if you got a array with number as index you get the last index with this:
eg: $array[0] = "foo"; $array[1] = "foo2";
$lastKey = sizeof($array) - 1;
only a little help :)
kyle17-Mar-2003 11:49
The comment by 'someone out there' is wrong according to the warning. If I read the warning right, if there are elements with empty values, it will die.
look at array_keys, it does what is described there, and probably faster too. Then loop through that (since all numeric indices, simple for loop should work:
$keys=array_keys($myarr); for($x=0;$x<count($keys);$x++) { ... }
though foreach is probably recommended)
php4all at free dot fr12-Mar-2003 03:37
There is an other way to grag the key and the value of an Array:
<? foreach($myArray as $Key => $Value) { echo "Key:" . $Key; echo "Value:" . $Value; } ?>
Then you will have the key and the value of each element in the Array.
php4all
someone out there11-Mar-2003 10:42
here's a way to grab array keys:
reset($myArray);
while (current($myArray) !== false) { $myKey = key($myArray); echo $myKey; //do as you will with each key next($myArray); }
retestro_REMOVE at SPAM_esperanto dot org dot il02-Mar-2003 02:31
The docs do not specify this, but adding to the array using the brackets syntax: $my_array[] = $new_value; will not advance the internal pointer of the array. therefore, you cannot use current() to get the last value added or key() to get the key of the most recently added element.
You should do an end($my_array) to advance the internal pointer to the end ( as stated in one of the notes on end() ), then
$last_key = key($my_array); // will return the key $last_value = current($my_array); // will return the value
If you have no need in the key, $last_value = end($my_array) will also do the job.
- Sergey.
|
|
|
|