$array = explode(".", $row[copy]);
$a = $array.length -1;
I want to return the last element of this array but all i get from this is -1. Help would be much appreciated.
$array = explode(".", $row[copy]);
$a = $array.length -1;
I want to return the last element of this array but all i get from this is -1. Help would be much appreciated.
I think your second line should be more like:
$index = count($array) - 1;
$a = $array[$index];
If you want an element from an array you need to use square brackets.
Try count:
$array = explode(".", $row[copy]);
$a = count($array) - 1;
$array[$a]; // last element
My PHP is a bit rusty, but shouldn't this be:
$array = explode(".", $row[$copy]);
$a = $array[count($array)];
i.e.: isn't a "$" missing in front of "copy", and does .length actually work?
As this is tag as PHP, I'll assume you are using PHP, if so then you'll want to do:
$array = explode(".", $row[copy]);
$a = count($array) - 1;
$value = $array[$a];
But this will only work if your keys are numeric and starting at 0.
If you want to get the last element of an array, but don't have numeric keys or they don't start at 0, then:
$array = explode(".", $row[copy]); $revArray = array_reverse($array, true); $value = $revArray[key($revArray)];
You could also use array_pop(). This function takes an array, removes the last element of the array and returns that element.
$array = explode(".", $row[copy]);
$a = array_pop($array);
This will modify the $array, removing the last element, so don't use it if you still need the array for something.
If you just want everythng after the final . you could try
$pos = strrpos($row['copy'], '.');
$str=($pos!==false) ? substr($row['copy'],$pos+1) : '';
This saves generating an array if all you needed was the last element.