note 99391 added to function.array-shift
| From: | ar at xonix dot ch | Date: | Sat, 14 Aug 2010 10:54:18 +0000 |
| Subject: | note 99391 added to function.array-shift | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171673@lists.php.net to get a copy of this message | ||
If you need the first or last entry of an array, then this could help you.
<?php
function array_last_entry($arr){
if(!is_array($arr))
return;
if(empty($arr))
return;
return end($arr);
}
function array_first_entry($arr){
if(!is_array($arr))
return;
if(empty($arr))
return;
reset($arr);
return current($arr);
}
$arr = array( '5' => 'five', '3' => 'three',
'8' => 'eight',);
echo 'last entry: '.array_last_entry($arr).'<br>';
echo 'first entry: '.array_first_entry($arr).'<br>';
echo 'alternative output:<br>';
echo 'last entry: '.$arr[count($arr)-1];
echo '<br>first entry: '.$arr[0];
?>
The output will look like:
last entry: eight
first entry: five
alternative output:
last entry:
first entry:
As you can see, if you have to handle arrays with non-continuous indexes, these functions may be
very helpful.
----
Server IP: 212.124.37.9
Probable Submitter: 83.79.181.6
----
Manual Page -- http://www.php.net/manual/en/function.array-shift.php
Edit -- https://master.php.net/note/edit/99391
Del: integrated -- https://master.php.net/note/delete/99391/integrated
Del: useless -- https://master.php.net/note/delete/99391/useless
Del: bad code -- https://master.php.net/note/delete/99391/bad+code
Del: spam -- https://master.php.net/note/delete/99391/spam
Del: non-english -- https://master.php.net/note/delete/99391/non-english
Del: in docs -- https://master.php.net/note/delete/99391/in+docs
Del: other reasons-- https://master.php.net/note/delete/99391
Reject -- https://master.php.net/note/reject/99391
Search -- https://master.php.net/manage/user-notes.php