note 28374 deleted from ref.array by sniper
| From: | sniper@php.net | Date: | Sat, 18 Oct 2003 18:02:25 +0000 |
| Subject: | note 28374 deleted from ref.array by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58831@lists.php.net to get a copy of this message | ||
Note Submitter: brian@exclamationlabs.com
----
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!