note 28374 added to ref.array
| From: | brian at exclamationlabs dot com | Date: | Fri, 10 Jan 2003 22:47:27 +0000 |
| Subject: | note 28374 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41977@lists.php.net to get a copy of this message | ||
Frederic Maybaum in newsgroups gave me this solution.
If you are coming from perl and want to get the -1, -2, etc of an array, you don't have this
convention in PHP, there is no $array[-1].
So here are 2 solutions:
$stuff = array('lard','utensil','pig','roasting spit');
to get 'roasting spit' you can do this:
print end($stuff) ."\n";
If your looking for something a little more flexible:
print $stuff[count($stuff) - 1] ."\n"; // 'roasting spit'
print $stuff[count($stuff) - 2] ."\n"; // 'pig'
This is handy if you are looking for a quick method to grab a filename from a fully qualified
filename variable:
$filename = "/usr/local/proggy/bin/goober";
print end(preg_split("|\/|",$filename)) ."\n"; // <-- goober
Thanks Frederic!
--
http://www.php.net/manual/en/ref.array.php
http://master.php.net/manage/user-notes.php?action=edit+28374
http://master.php.net/manage/user-notes.php?action=delete+28374
http://master.php.net/manage/user-notes.php?action=reject+28374