Shortest Code to Translate a PHP array

5

1

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?

Rick Buczynski

Posted 2014-11-25T19:26:10.337

Reputation: 163

2

Hello, welcome to PPCG :) Presumably, you want the shortest code to do this? If so, I recommend tagging your problem [tag:code-golf] (Currently this lacks a winning criterion and could be closed). Consider using our sandbox for future questions.

– FryAmTheEggman – 2014-11-25T19:34:50.743

Thanks @FryAmTheEggman - I've updated my tag, and am reading up on the sandbox now. – Rick Buczynski – 2014-11-25T19:36:19.310

1No 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 – 2014-11-25T19:43:07.473

Got it. Will do. – Rick Buczynski – 2014-11-25T19:43:45.460

1This question isn't migrated: it's cross-posted, and it's already been answered on SO. – Peter Taylor – 2014-11-25T20:29:12.567

Yea, I don't know how to do that/I don't have authority to. How do suggest I handle this? – Rick Buczynski – 2014-11-25T20:36:49.983

3If 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 – 2014-11-25T22:56:07.607

Answers

7

explode('&',http_build_query($a))

33 bytes

Devon

Posted 2014-11-25T19:26:10.337

Reputation: 186

Note that http_build_query() will encode special characters like &. http://rextester.com/ZFSIX69274

– Paul Spiegel – 2017-08-30T19:23:06.113

PHP actually has an explode function? Cool. :D – Kasran – 2014-11-25T19:31:24.040

2

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 ;)

– FryAmTheEggman – 2014-11-25T20:08:04.533

@FryAmTheEggman. Thanks, it looks like vBuck modified it for me. New to codegolf, just copied my answer from SO tbh. :) – Devon – 2014-11-25T21:26:16.403

split is one character shorter than explode (not the same, but in this case functionally equivalent). – primo – 2014-11-26T00:05:26.960

@Devon Whenever you copy your answer from somewhere else, you should always give credit to the original source. Could you edit that in? – isaacg – 2014-11-26T00:05:55.900

1@isaacg, I copied it from myself lol. – Devon – 2014-11-26T00:46:51.433

@primo, split is deprecated as of 5.3 – Devon – 2014-11-26T00:48:06.373

@Devon E_DEPRECATED is of no consequence to code golf. – primo – 2014-11-26T03:04:25.217

@primo, okay Thanks, I think I'm understanding how this site works now. – Devon – 2014-11-26T03:15:57.343

5

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.

Ismael Miguel

Posted 2014-11-25T19:26:10.337

Reputation: 6 797

2Nice 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. – Rick Buczynski – 2014-11-29T03:12:02.423

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 – 2014-11-29T03:16:04.933

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 – 2014-11-29T03:21:54.610

@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 – 2014-11-29T03:34:19.900

1

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

ArtisticPhoenix

Posted 2014-11-25T19:26:10.337

Reputation: 329