Reputation: 8405
After much search (and finding endless posts about multidims, but no single dims) I thought I'd ask this question.
I have an array
$arr = array('foo' => 'bar');
and am looking for an output of
$str = 'foo bar';
This MUST be a one liner, no recursive loops etc etc etc, I am thinking that its going to have to be a lambda of some sort or another. This array will NEVER have more than a single key and a single value though.
I think its going to end up looking something like
$arr = array('foo' => 'bar');
echo 'Authorization: ' . array_walk($arr, function ($v, $k) { echo "$k $v"; });
which unfortunately ends up as foo barAuthorization: 1
no idea where the 1 comes from =P
Upvotes: 1
Views: 71
Reputation: 71414
This should be quite easy since the array was just initialized and the pointer resides at the beginning of the array:
echo 'Authorization: ' . key($arr) . ' ' . current($arr);
Of course if you have already read data from the array you would want to do a reset()
before doing this to return the pointer to the beginning of the array.
Upvotes: 7
Reputation: 28419
"This array will NEVER have more than a single key and a single value though"
echo 'Authorization: ' . array_shift(array_keys($arr)). ' ' . array_shift($arr) ;
Upvotes: -3
Reputation: 3204
PHP processes the function before it processes the string in front of it. Try replacing echo
in your function with return
. I think the 1 comes from the successfull processing of the array_walk function.
echo 'Authorization: ' . array_walk($arr, function ($v, $k) { return "$k $v"; });
UPDATE: Check out Example#1 http://php.net/manual/en/function.key.php
Upvotes: -1