# Shortest Code to Translate a PHP array

## NOTE: This question was migrated from https://stackoverflow.com/q/27134588/1442685

PHP QUESTION

Almost purely out of curiousity, how can I take an array like this:

array( 'fruit' => 'banana', 'drink' => 'milk')


And convert it to an array like this:

array( 'fruit=banana', 'drink=milk')


Requirements

• Cannot use anonymous functions
• If needed, can only use array_* functions that accept callable if that callable is native to PHP
• Must be a one-liner solution

This is not for a class. I'm curious to see how clever one can be with array functions. Given the requirements, is it possible?

• Hello, welcome to PPCG :) Presumably, you want the shortest code to do this? If so, I recommend tagging your problem code-golf (Currently this lacks a winning criterion and could be closed). Consider using our sandbox for future questions. – FryAmTheEggman Nov 25 '14 at 19:34
• Thanks @FryAmTheEggman - I've updated my tag, and am reading up on the sandbox now. – Rick Buczynski Nov 25 '14 at 19:36
• No problem :) Just some nit-picking, you might want to edit your title to reflect the rules i.e. "Shortest Code to translate a PHP array." You also shouldn't say "preferably"; either force one-liners, or don't. Also, just like SO, wait a while before accepting ;p – FryAmTheEggman Nov 25 '14 at 19:43
• This question isn't migrated: it's cross-posted, and it's already been answered on SO. – Peter Taylor Nov 25 '14 at 20:29
• If you want to migrate a question from one site to another within the Stack Exchange network, you should use a custom flag. But 99% of the time you should pay no attention to people on SO who say that a question would be a better fit on PPCG, because most of them have no idea what's on topic here and what isn't. – Peter Taylor Nov 25 '14 at 22:56

• I totally disagree with point 2. PHP is a free-form language (most of the whitespace don't matter) and the braces are optional for 1 expression. And the foreach can be implemented as an expression using eval('foreach($a as$k=>$v)$z[]="$k=$v";return$z;');, but DO NOT USE THIS NO MATTER WHAT! But still, I wasn't expecting to win this one: I just wanted to show another solution. But mine as the advantage of keeping a copy of the old array. – Ismael Miguel Nov 29 '14 at 3:16 • Hmm, well that's a clever way to evaluate a loop. I'll give you that much :) If memory footprint was a requirement, I wonder how the loop + 2 arrays would stack against the consumption of @Devon's code. – Rick Buczynski Nov 29 '14 at 3:21 • @vBuck It will consume around +1kb than the other solution. But nothing serious. About cpu cycles, mine probably would be a tiny mini bit faster. The overhead of calling 2 functions + looking for a substring + storing it's multiple parts and so on vs. a single iteration over 2 keys. – Ismael Miguel Nov 29 '14 at 3:34 # 32 bytes foreach($a as$k=>&$v)$v="$k=\$v";


Update by reference, shaves off 1 byte.

Not sure how important removing the original key is, sort of a grey area.

This is what I mean:

[
['fruit'] => 'fruit=banana',
['drink'] => 'drink=milk'
]


But it saves on variables, as your not creating a new array