-6

For instance, I have an array:

$x = array("a", "b", "c", "d", "e");

Is there any function to iterate all values of array and duplicate the values into:

$x = array("a", "a", "b", "b", "c", "c", "d", "d", "e", "e");

I've not found any related solution after googled it for a while.

Thanks so much!

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Similar: https://stackoverflow.com/q/8234899/2943403 – mickmackusa Mar 08 '19 at 08:02
  • 1
    What is your actual use for this technique? Storing redundant data doesn't smell of best practices. What are you _actually_ doing? – mickmackusa Mar 08 '19 at 08:04
  • it's not that similar dude! It's hard to explain but my project requires some conditions that I gotta double the values so I can read them twice when I foreach. Sorry if there's another more effective way to get it done but right now I just think of it. – Long Jackson Mar 08 '19 at 08:52
  • It is exactly the same except for the element order. Adjusting your data is not the solution to your coding problem. – mickmackusa Mar 08 '19 at 08:52
  • 1
    *so I can read them twice when I foreach*. Why not just do the same thing twice in the loop? `foreach($a as $b){ echo $b; echo $b;}` – Andreas Mar 08 '19 at 08:53
  • No no. It's like I have two fields and I gotta echo a unique value for both fields each time I foreach. – Long Jackson Mar 08 '19 at 09:05
  • 1
    It is never too late to clarify your coding problem. I have voted to close as Unclear. It is unclear by your posted question and sample data exactly what you need help with. – mickmackusa Mar 08 '19 at 09:49
  • 1
    @mickmackusa I think you're on the right track here. Closer inspection of this question smells strongly of an [XY Problem](http://xyproblem.info/) – Phil Mar 08 '19 at 09:51

3 Answers3

4

Looks like a reasonably simple reduction (using array_reduce())

$x = array_reduce($x, function($arr, $val) {
    array_push($arr, $val, $val);
    return $arr;
}, []);

Demo ~ https://3v4l.org/eNH8a


Just realised that "reduction" sounds a bit funny since we're making the array bigger. Think of it more as a transformation. See https://en.wikipedia.org/wiki/Reduce_(parallel_pattern)

Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    I recommend change the body of the reduce function to: `return array_merge($arr, array_pad([], $times, $val));` - this way the `$times` can be change easily - add `use ($times)` to the reduce function declaration – dWinder Mar 08 '19 at 09:14
  • @dWinder I see what you mean. I feel I could do something more concisely using the PHP _splat_ operator though. I'll have a think about it – Phil Mar 08 '19 at 09:50
1

Iterating over the array and storing the values in a new array is a simple solution.

$x =  array("a", "b", "c", "d", "e");
$result_array = [];
$repeat_x_times = 2;

foreach ($x as $element) {
    for ($i = 0; $i < $repeat_x_times ; $i++) {
        $result_array[] = $element;
    }
}
Raul Sauco
  • 2,645
  • 3
  • 19
  • 22
0

Not sure if there is a library function for this, but are you looking for something along the lines of this:

function duplicate_array($array=array()) {
    $result = array();
    foreach($array as $elem) {
        $result[] = $elem;
        $result[] = $elem;
    }

    return $result;
}

This should in theory do what you want.

$x = array("a", "b", "c", "d", "e");

var_dump($x);

$x = duplicate_array($x);

var_dump($x);

This should output:

array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "e"
}
array(10) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "a"
  [2]=>
  string(1) "b"
  [3]=>
  string(1) "b"
  [4]=>
  string(1) "c"
  [5]=>
  string(1) "c"
  [6]=>
  string(1) "d"
  [7]=>
  string(1) "d"
  [8]=>
  string(1) "e"
  [9]=>
  string(1) "e"
}