any idea how if the following is possible in PHP as a single line ?:
... It doesn
Either current($array)
or array_shift($array)
will work, the former will leave the array intact.
I actually use a convenience function i wrote for such purposes:
/**
* Grabs an element from an array using a key much like array_pop
*/
function array_key_value($array, $key) {
if(!empty($array) && array_key_exists($key, $array)) {
return $array[$key];
}
else {
return FALSE;
}
}
then you just call it like so:
$result = array_key_value(getMeAnArray(), 'arrayKey');
http://us3.php.net/reset
Only available in php version 5.
Try:
<?php
$firstElement = reset(functionThatReturnsAnArray());
If you're just looking for the first element of the array.
nickf, good to know, thanks. Unfortunately that has readability problems beyond a few commas.
Well, I have found a couple of ways to get what you want without calling another function.
$firstElement = ($t = functionThatReturnsAnArray()) ? $t[0] : false;
and for strings you could use
$string = (($t = functionThatReturnsAnArray())==0) . $t[0];
.. Interesting problem
Draco