Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : end

end

Set the internal pointer of an array to its last element (PHP 4, PHP 5)
mixed end ( array &array )

Example 304. A simple end() example

<?php

$fruits
= array('apple', 'banana', 'cranberry');
echo
end($fruits); // cranberry

?>

Related Examples ( Source code ) » end
















Code Examples / Notes » end

29-aug-2002 02:17

When adding an element to an array, it may be interesting to know with which key it was added. Just adding an element does not change the current position in the array, so calling key() won't return the correct key value; you must first position at end() of the array:
function array_add(&$array, $value) {
$array[] = $value; // add an element
end($array); // important!
return key($array);
}


jasper

This function returns the value at the end of the array, but you may sometimes be interested in the key at the end of the array, particularly when working with non integer indexed arrays:
<?php
// Returns the key at the end of the array
function endKey($array){
end($array);
return key($array);
}
?>
Usage example:
<?php
$a = array("one" => "apple", "two" => "orange", "three" => "pear");
echo endKey($a); // will output "three"
?>


sven arduwie

RE: mika dot stenberg at helsinki dot fi
Your function array_set_current is flawed:
function array_set_current(&$array, $key){
  reset($array);
  while(current($array)){
      if(key($array) == $key){
          break;
      }
      next($array);
  }
}
Notice that current() cannot distinguish the end of an array from a boolean FALSE element value. For example, try this:
$array = array(false, 'This', 'is', 'a', 'test');
array_set_current($array, 3);
var_dump(current($array));
This will work:
function array_set_current(&$array, $key) {
reset($array);
foreach ($array as $value) {
if (key($array) === $key) {
break;
}
}
}
Also, I prefer key($array) === $key over key($array) == $key.


egingell

Posted by johnny at west dot net
03-Nov-2006 06:45
>> An easier way to get the last key:
>> $lastkey = end(array_keys($arr));
As stated in a previous note, this may not function as expected (or at all) as end() takes the parameter by reference and you cannot pass a function call by reference.


ken

Please note that from version 5.0.4 ==> 5.0.5 that this function now takes an array. This will possibly break some code for instance:
<?php
echo ">> ".end(array_keys(array('x' => 'y')))."\n";
?>
which will return "Fatal error: Only variables can be passed by reference" in version <= 5.0.4 but not in 5.0.5.
If you run into this problem with nested function calls, then an easy workaround is to assign the result from array_keys (or whatever function) to an intermediary variable:
<?php
$x = array_keys(array('x' => 'y'));
echo ">> ".end($x)."\n";
?>


user

It may be obvious, but if the array is empty, end() returns bool(false).

flavio ventura

In some previous post you can use
current($array)
instead of
$array[key($array)]
I think it's more self-explaining.


29-aug-2002 02:34

If you need to get a reference on the first or last element of an array, use these functions because reset() and end() only return you a copy that you cannot dereference directly:
function first(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
reset($array);
return &$array[key($array)];
}
function last(&$array) {
if (!is_array($array)) return &$array;
if (!count($array)) return null;
end($array);
return &$array[key($array)];
}


mika dot stenberg

Here's something useful with arrays that I did: change an items position up or down in array. Thanks to previous posters for some useful functions (included):
// $mode -1 (negat) moves the item down, +1 (pos) moves the item up
// I used this because i parsed the mode from the query string
// You get the picture though
// $from_id points which item to move
function change_pos($array, $from_id, $mode){
// get hold of the beginning and the end
$start = first($array);
$end = last($array);
reset($array);
// find the position in an array
array_set_current($array, $from_id);
// remember the old value
$temp = $archive[ $from_id ];
// move down
if ($uusi_id < 0){


   if (key($array) != $end) {
           $array[ $from_id ] = next($array);
           $array[key($aray)] = $temp;
         }
   else echo "Couldnt move it!";
}
// move up
else if ( key($array) != $start ) {
   $array[ $from_id ] = prev($array);
   $array[key($array)] = $temp;
   save($array);
   }
else echo "Couldnt do it !";
}
function array_set_current(&$array, $key){
  reset($array);
  while(current($array)){
      if(key($array) == $key){
          break;
      }
      next($array);
  }
}
function first(&$array) {
if (!is_array($array)) return null;
if (!count($array)) return null;
reset($array);
return key($array);
}
function last(&$array) {
if (!is_array($array)) return null;
if (!count($array)) return null;
end($array);
return key($array);
}


12-may-2005 09:21

Fix for unknown writer of 29-aug-2002.
If you need to get a reference to the first or last element of an array, use these functions:
   function &first(&$array) {
if (!is_array($array))
return null;
if (!count($array))
return false; // like reset()
reset($array);
return $array[key($array)];
}

function &last(&$array) {
if (!is_array($array))
return null;
if (!count($array))
return false; // like end()
end($array);
return $array[key($array)];
}
Example (watch out - use as &last() and &first()):
$a = array( 0 => 'less', 10 => 'ten', 20 => ' more');
if ($first = & first($a)) // not false or null
   $first = 'nothing';
if ($last = & last($a)) // not false or null
   $last = 'double';
Now $a is array( 0 => 'nothing', 10 => 'ten', 20 => ' double')


johnny

An easier way to get the last key:
$lastkey = end(array_keys($arr));


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker