关于PHP:PHP – 数组中的重复值

PHP - Duplicate values in an array

说我有这个密码

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

是否有任何函数可以在创建后调用以复制值,因此在上面的示例中,$x将变为

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

我想是这样的,但不起作用

$X=$X+$X;


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

$x = array_merge($x,$x);

将一个数组合并到其自身上将按顺序重复这些值。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
php > $x = array("a","b","c","d","e");
php > print_r(array_merge($x, $x));

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => a
    [6] => b
    [7] => c
    [8] => d
    [9] => e
)


这应该可以做到:

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


1
$x = array_merge($x, $x);

如果你愿意的话,你也可以去做循环和复制。


您可以循环遍历数组,并将每个变量循环到单独的重复数组。下面是一些代码:

1
2
3
4
5
6
7
8
9
10
$x = array("a","b","c","d","e");
$duplicateArray = $array;

foreach ($x as $key) {
    $duplicateArray[] = $key;
}

foreach ($x as $key) {
    $duplicateArray[] = $key;
}