6

我得到以下数组:

$arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2');

问题是,当我使用 时array_merge( (array) "Select the data", $arr);,它确实将数组键更改为:

Array
(
    [0] => Not specified
    [1] => Somedata
    [2] => Somedata1
    [3] => Somedata2
)

是否可以跳过array_mergekey preversion 使输出看起来像这样?

Array
(
    [0] => Not specified
    [6] => Somedata
    [7] => Somedata1
    [8] => Somedata2
)
4

1 回答 1

11

使用+运算符创建 2 个数组的并集:

$arr = array(6 => 'Somedata', 7 => 'Somedata1', 8 => 'Somedata2');

$result = (array)'Select the data' + $arr;

var_dump($result);

结果:

array(4) {
  [0]=>
  string(15) "Select the data"
  [6]=>
  string(8) "Somedata"
  [7]=>
  string(9) "Somedata1"
  [8]=>
  string(9) "Somedata2"
}
于 2012-09-12T23:48:27.237 回答