Function Returns List, But I Only Need One Value
Hi. I think this is my first real post to this community, though I've been lurking for a while and have probably commented on a few posts here and there. I hope this isn't too long; I tried to find a good place to lj-cut it, and couldn't really. Let me know if it's a problem, and I'll cut it anyway.
I routinely have situations where I have a function return an array indexed by name (an associative array or hash, for those who also speak Perl), and I don't need the entire array. I really just need one value. For example, let's say I have a function "somefunc()", which returns an array like:
I simply want to get the value of key2 into a single string variable, $somevar. Now, in Perl, I could do this all in one shot, like so:
So now $somevar equals 'bar', and I didn't have to use any extra variables. I'd like to be able to do the same thing in PHP, but so far, the best I can come up with is:
This works — after all, $somevar now equals 'bar', just like I want — but it also leaves $temp_var hanging around, cluttering up the namespace and wasting memory. I'd like to avoid that. Sure, in this particular (trivial) case, the memory wasted is only a few bytes, but in a real program of larger scale, the hash returned might be much larger. And this sort of thing might be happening dozens or hundreds of times (say, in a foreach() loop that iterates over a large number of options)... it could really add up.
Does anyone know how I can grab just the value I want from a function return? I've looked in the various array functions, but none of them seem to help. In some situations, I can do:
but this will fail if the array returned by somefunc() doesn't have unique values. For example, if it looks like:
then the version returned by the array_flip() call will have a collision in the two 'foo' keys — I think the value 'key4' will clobber 'key1', but it might be the reverse, or it might be unpredictable. I haven't even bothered to check what the behavior is, because it's so obviously not a solution I can use.
This has been driving me somewhat nuts, so any help anyone can provide would be much appreciated. Thanks in advance.
I routinely have situations where I have a function return an array indexed by name (an associative array or hash, for those who also speak Perl), and I don't need the entire array. I really just need one value. For example, let's say I have a function "somefunc()", which returns an array like:
Array
(
[key1] => foo
[key2] => bar
[key3] => baz
)I simply want to get the value of key2 into a single string variable, $somevar. Now, in Perl, I could do this all in one shot, like so:
$somevar = {somefunc($arg1, $arg2)}->{'key2'}So now $somevar equals 'bar', and I didn't have to use any extra variables. I'd like to be able to do the same thing in PHP, but so far, the best I can come up with is:
$temp_var = somefunc($arg1, $arg2);
$somevar = $temp_var{'key2'};This works — after all, $somevar now equals 'bar', just like I want — but it also leaves $temp_var hanging around, cluttering up the namespace and wasting memory. I'd like to avoid that. Sure, in this particular (trivial) case, the memory wasted is only a few bytes, but in a real program of larger scale, the hash returned might be much larger. And this sort of thing might be happening dozens or hundreds of times (say, in a foreach() loop that iterates over a large number of options)... it could really add up.
Does anyone know how I can grab just the value I want from a function return? I've looked in the various array functions, but none of them seem to help. In some situations, I can do:
$somevar = array_search('key2', array_flip(somefunc($arg1, $arg2)));but this will fail if the array returned by somefunc() doesn't have unique values. For example, if it looks like:
Array
(
[key1] => foo
[key2] => bar
[key3] => baz
[key4] => foo
)then the version returned by the array_flip() call will have a collision in the two 'foo' keys — I think the value 'key4' will clobber 'key1', but it might be the reverse, or it might be unpredictable. I haven't even bothered to check what the behavior is, because it's so obviously not a solution I can use.
This has been driving me somewhat nuts, so any help anyone can provide would be much appreciated. Thanks in advance.
