0

Suppose the array:

$prices = array (
  '7.20',
  '7.40',
  '8.00',
  '8.50',
  '9.30',
  '10.40',
  '11.00',
  '12.00',
  '12.50',
  '13.00',
)

Is there any clever, short (as close to an one-liner as possible) solution to copy each value to the next position, effectively doubling the array? So that the array above would become:

$prices = array (
  '7.20',
  '7.20',
  '7.40',
  '7.40',
  '8.00',
  '8.00',
  '8.50',
  '8.50',
  '9.30',
  '9.30',
  '10.40',
  '10.40',
  '11.00',
  '11.00',
  '12.00',
  '12.00',
  '12.50',
  '12.50',
  '13.00',
  '13.00',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Faye D.
  • 833
  • 1
  • 3
  • 16
  • Is the objective to obtain specifically that result with the data in that order, or is it primarily just to double the size of the array? Also, have you tried anything already? – ADyson Feb 07 '23 at 23:10
  • Yup, I want the resulting array to have its values in this particular order because I need to use them along with the values of another array, using `array_combine` to produce a new array that will serve a specific purpose. I've done it with a bunch of `array_splice`'s but I was wondering if there is a shorter/simpler solution... – Faye D. Feb 07 '23 at 23:23
  • Just merge the array with itself: `$prices = array_merge($prices, $prices);` and then sort it: `sort($prices);` – Alex Howansky Feb 07 '23 at 23:25
  • Related: [How to duplicate values in a flat, indexed array? (Append all elements to end of array in order)](https://stackoverflow.com/q/8234899/2943403) and [How to duplicate every key in php array](https://stackoverflow.com/q/68666816/2943403) andn [Duplicate string x amount of times inside an array PHP](https://stackoverflow.com/q/68909837/2943403) – mickmackusa Feb 08 '23 at 00:55
  • And content on [this page](https://stackoverflow.com/q/11827451/2943403) can be modified slightly to suit this question. – mickmackusa Feb 08 '23 at 01:16
  • 1
    Clever, short, one-line solutions usually turn out to be unreadable, and can behave in unexpected ways. Just use a loop. If you want to make it look "complicated and impressive" then stuff it all into the loop structure. `for($i=0,$o=[],$c=count($arr);$i<$c;$o[]=$arr[$i],$o[]=$arr[$i],++$i);` Which is as unreadable as anyone else's answer, but isn't overcomplicated and won't blow out your stack unexpectedly. – Sammitch Feb 08 '23 at 02:10

2 Answers2

2

Here's one approach. The strategy is to (in a one-liner):

  1. Use array_map with a callback defined inline, to produce a "2D" array where each element is a pair of copies of the corresponding element in the given array.

  2. Use array_merge(...$a) to "flatten" that (as per https://stackoverflow.com/a/46861938/765091).

$prices = array ('7.20', '7.40', '8.00', '8.50', '9.30', '10.40', '11.00', '12.00', '12.50', '13.00',);

$res = array_merge(...array_map(function ($e) {return [$e, $e];}, $prices));

echo(implode($res, ', '));

prints:

7.20, 7.20, 7.40, 7.40, 8.00, 8.00, 8.50, 8.50, 9.30, 9.30, 10.40, 10.40, 11.00, 11.00, 12.00, 12.00, 12.50, 12.50, 13.00, 13.00
slothrop
  • 3,218
  • 1
  • 18
  • 11
1

The old "transpose and flatten" technique. Just feed the input array to array_map() twice.

Code: (Demo -- includes other techniques too)

var_export(array_merge(...array_map(null, $prices, $prices)));
mickmackusa
  • 43,625
  • 12
  • 83
  • 136