Alex Rivera | Logout

How to duplicate values in a flat, indexed array? (Append all elements to end of array in order)

Asked 2011-11-22T22:37:32.870
13

Say I had this code

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

Is there any function that I could call after creation to duplicate the values, so in the above example $x would become

array("a", "b", "c", "d", "e", "a", "b", "c", "d", "e");

I thought something like the following but it doesn't work.

$x = $x + $x;
Edit
Report

2 Answers

2

This should do the trick:

$x = array("a", "b", "c", "d", "e");
$x = array_merge($x,$x);
answered 2011-11-22T22:41:15.663
0
$x = array_merge($x, $x);

Or you could go looping and duplicating, if you preferred.

answered 2011-11-22T22:47:24.343

Your Answer