这是我的旧阵列。
$oldarray = Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => stdClass Object
(
[0] => http://test.to/getae/vidzichawal1
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
)
)
我想与这个新数组合并:
$newarray = Array
(
[mock] => Array
(
[0] => http://test.to/getae/vidzichawal2
)
)
我正在通过array_merge_recursive($oldarray, $newarray);
结果如下:
Array
(
[0] => http://test.to/getac/l4p0y6ziqt9h
[mock] => Array
(
[0] => http://test.to/getae/vidzi5
[1] => http://test.to/getae/vidzi6
[4] => http://test.to/getae/1x5fbr9t64xn
[2] => http://test.to/getae/vidzi7
[0] => http://test.to/getae/vidzichawal1
)
);
所有的东西都运行良好,但有一个问题,你可以看到结果有双0键,当我在循环中使用此链接仅1链接反向0我想自动设置此键0 1 2 3 4 5 6并在合并后继续。
我希望你理解我想要的东西谢谢
答案 0 :(得分:0)
使用array_merge()
<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>
以上示例将输出:
Array
(
[color] => green
[0] => 2
[1] => 4
[2] => a
[3] => b
[shape] => trapezoid
[4] => 4
)
注意您不能拥有重复的密钥!
<强>更新强>
使用array_merge_recursive()
<?php
$oldarray = array('http://test.to/getac/l4p0y6ziqt9h', 'mock' => array('http://test.to/getae/vidzichawal1', 'http://test.to/getae/vidzi6', 'http://test.to/getae/1x5fbr9t64xn', 'http://test.to/getae/vidzi7'));
$newarray = array('mock' => array('http://test.to/getae/vidzichawal2'));
$result = array_merge_recursive($oldarray, $newarray);
var_dump($result);
?>
输出
array (size=2)
0 => string 'http://test.to/getac/l4p0y6ziqt9h' (length=33)
'mock' =>
array (size=5)
0 => string 'http://test.to/getae/vidzichawal1' (length=33)
1 => string 'http://test.to/getae/vidzi6' (length=27)
2 => string 'http://test.to/getae/1x5fbr9t64xn' (length=33)
3 => string 'http://test.to/getae/vidzi7' (length=27)
4 => string 'http://test.to/getae/vidzichawal2' (length=32)