5
\$\begingroup\$

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?

\$\endgroup\$
7
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Nov 25, 2014 at 19:34
  • \$\begingroup\$ Thanks @FryAmTheEggman - I've updated my tag, and am reading up on the sandbox now. \$\endgroup\$ Nov 25, 2014 at 19:36
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Nov 25, 2014 at 19:43
  • 1
    \$\begingroup\$ This question isn't migrated: it's cross-posted, and it's already been answered on SO. \$\endgroup\$ Nov 25, 2014 at 20:29
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Nov 25, 2014 at 22:56

3 Answers 3

7
\$\begingroup\$
explode('&',http_build_query($a))

33 bytes

\$\endgroup\$
10
  • \$\begingroup\$ PHP actually has an explode function? Cool. :D \$\endgroup\$
    – Kasran
    Nov 25, 2014 at 19:31
  • 2
    \$\begingroup\$ By the way, since this is code-golf, you should update your answer with the length of your code in bytes. Also, I think you can remove that space character ;) \$\endgroup\$ Nov 25, 2014 at 20:08
  • \$\begingroup\$ @FryAmTheEggman. Thanks, it looks like vBuck modified it for me. New to codegolf, just copied my answer from SO tbh. :) \$\endgroup\$
    – Devon
    Nov 25, 2014 at 21:26
  • \$\begingroup\$ split is one character shorter than explode (not the same, but in this case functionally equivalent). \$\endgroup\$
    – primo
    Nov 26, 2014 at 0:05
  • 1
    \$\begingroup\$ @isaacg, I copied it from myself lol. \$\endgroup\$
    – Devon
    Nov 26, 2014 at 0:46
5
\$\begingroup\$

PHP (33 bytes)

I am not 100% sure if this is valid.

I used 0 (zero) functions.

This solution is 1 byte shorter than @Devon's solution.

The code:

foreach($a as$k=>$v)$z[]="$k=$v";

The var $a is the array, the var $z will have the new array.

There are no restrictions to the vars or how it should be implemented.

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Nice solution. It does work, but @Devon's answer still takes the cake on the following grounds: 1). Easier to read; 2). It's truly a 'one-liner' solution, where your solution requires multiple lines to implement -- ie: foreach cannot be evaluated as part of an expression, and the addition of $z requires another line for assignment or use. \$\endgroup\$ Nov 29, 2014 at 3:12
  • \$\begingroup\$ 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. \$\endgroup\$ Nov 29, 2014 at 3:16
  • \$\begingroup\$ 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. \$\endgroup\$ Nov 29, 2014 at 3:21
  • \$\begingroup\$ @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. \$\endgroup\$ Nov 29, 2014 at 3:34
1
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.