array_merge改变键 [英] array_merge changes the keys

查看:312
本文介绍了array_merge改变键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了以下数组:

  $ ARR =阵列(6 =>'Somedata',7 =>'Somedata1',8 =>'Somedata2');

问题是,当我使用 array_merge((阵列)选择数据,$ ARR); ,它确实改变了数组键为:

 阵列

    [0] =>未标明
    [1] => Somedata
    [2] => Somedata1
    [3] => Somedata2

这有可能跳过 array_merge 键preversion所以输出会是什么样子呢?

 阵列

    [0] =>未标明
    [6] => Somedata
    [7] => Somedata1
    [8] => Somedata2


解决方案

使用 + 运营商创建2阵列的工会:

  $ ARR =阵列(6 =>'Somedata',7 =>'Somedata1',8 =>'Somedata2');$结果=(阵列)'选择数据'+ $改编;后续代码var_dump($结果);

结果:

 阵列(4){
  [0] =>
  串(15)选择数据
  [6] =>
  串(8)Somedata
  [7] =>
  串(9)Somedata1
  [8] =>
  串(9)Somedata2
}

I got the following array:

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

The problem is that, when I use array_merge( (array) "Select the data", $arr);, it does change the array keys into:

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

Is that possible to skip the array_merge key preversion so the output would look like this?

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

解决方案

Use the + operator to create a union of the 2 arrays:

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

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

var_dump($result);

Result:

array(4) {
  [0]=>
  string(15) "Select the data"
  [6]=>
  string(8) "Somedata"
  [7]=>
  string(9) "Somedata1"
  [8]=>
  string(9) "Somedata2"
}

这篇关于array_merge改变键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆