View Full Version : Resolved second from the end of array
james438
05-25-2010, 04:12 AM
I don't have any real need for this, but is there a built in php function that will allow me to get the second value from the end of an array? Sort of like:
$end=array($arr, -1, 1);
bluewalrus
05-25-2010, 04:19 AM
I've never used this but my guess is
$inversed = array_reverse($array);
$end = $inversed[1];
http://php.net/manual/en/function.array-reverse.php
james438
05-25-2010, 04:44 AM
Not exactly what I was looking for, but works just the same :).
djr33
05-25-2010, 05:14 AM
echo $array[count($array)-2];
(We subtract 2 because count returns a count from 1+ and array indexes range from 0+.)
BTW, this will return two errors (undefined index notice; error count's value is not an array) if you use it on a non-array or on an empty array. Check that first.
james438
05-25-2010, 06:03 AM
nice, thanks for those answers. I was looking for something really simple like the answers you both gave.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.