- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个代码
$a1 = array(
'success' => TRUE,
'data' => array(
'foo' =>
array(
21 =>
array(
1 =>
array(1, 2, 3, 4, 5)
)
)
)
);
$a2 = array(
'success' => TRUE,
'data' => array(
'foo' =>
array(
21 =>
array(
7 =>
array(6, 7, 8, 9, 10)
)
)
)
);
$results = array();
$results = array_merge_recursive($results, $a1['data']);
$results = array_merge_recursive($results, $a2['data']);
var_dump($results);
根据我对 array_merge_recursive() 的理解,我期望结果是
array
'foo' =>
array
21 =>
array
1 =>
array
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
7 =>
0 => int 6
1 => int 7
2 => int 8
3 => int 9
4 => int 10
相反,我得到了这个
array
'foo' =>
array
21 =>
array
1 =>
array
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
22 =>
array
7 =>
array
0 => int 6
1 => int 7
2 => int 8
3 => int 9
4 => int 10
22
索引从何而来?为什么输出不同?我是不是用错了函数?
最佳答案
array_merge_recursive
从与第一个数组相同的深度合并元素/数组,但如果两个数组的键都是数字索引并且它们相同,则它会附加到它。这就是您的情况。从那时起,您的数组将附加到第二级,其中通过创建索引 22
找到索引 21
。要接收所需的输出,您已将索引 21
更改为字符串键,如 "x21"
php 手册中的注释
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
关于PHP - 意外的 array_merge_recursive() 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17268734/
所以我想从一个文本文件动态构建一个多维数组,除了数字键让我感到困惑之外,一切都完美无缺...... 文本文件看起来像这样: a=1 b.c=2 b.d.0.e=3 b.d.0.f=4 b.d.1.e=
所以基本上我有这两个数组我想合并...... array(1) { ["first"]=> array(1) { ["second"]=> arra
我已经浏览了这里的帖子,但找不到任何似乎可以解决我的问题的内容。我对 PHP/MySQL 相当陌生,我似乎无法弄清楚为什么这个 array_merge_recursive 函数一直告诉我我的第一个数组
$array1 = [ '1' => '11', 'b' => 1, 3 => 33, 8 => 8 ]; $array2 = [ '1' => '22
$php -a php > $data1 = ['tag' => 'div', 'classes' => [1,2,3]]; php > $data2 = ['tag' => 'section', '
我正在为 PHP 中的数据结构而苦苦挣扎。我正在尝试使用 array_merge_recursive 通过类似的键来压缩数组,然后获取所有值而不是覆盖它们。这就是我选择 array_merge_rec
我只想合并 $a = array("59745506"=>array("up" => 0,)); $b = array("59745506"=>array("text" => "jfrj")); $c
php中array_merge_recursive函数用于将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面,返回作为结果的数组。本文章将详细介绍array_merge_recur
我有这个代码 $a1 = array( 'success' => TRUE, 'data' => array( 'foo' => ar
我尝试递归合并以下 2 个数组,这样我就不会丢失数据,但我也不希望重复任何数据。 $a = array( 'group1' => array( 'names' => array
是否有递归合并数组的方法,就像 PHP 的 array_merge_recursive() 函数一样,除了整数键被视为字符串键? (对于过程而言,键保持可解析为整数很重要。) 例如: $a = arr
我有一些默认配置,以及一些可配置的特定配置。我需要将特定配置合并到默认配置中。 在特定配置选项不存在的情况下, 将使用默认选项。 在 value 的情况下是标量,具体配置应该应用 在 value 的情
我是一名优秀的程序员,十分优秀!