我有两个数组
/**
* Menu Navigation
* @var array
*/
public $nav_top = array(
100 => 'Dashboard',
200 => 'Sell',
300 => 'Products',
400 => 'History',
500 => 'Customers',
600 => 'Setup'
);
/**
* Menu Navigation
* @var array
*/
public $nav_sub = array(
201 => 'Current Sale',
202 => 'Retrieve Sale',
203 => 'Close Register',
301 => 'Product',
302 => 'Stock Control',
303 => 'Price Books',
304 => 'Types',
305 => 'Suppliers',
306 => 'Brands',
307 => 'Tags',
501 => 'Customer',
502 => 'Group'
);
如何将这两个数组组合在一起,而不会失去它们的关键索引?如果使用
array_merge()
函数,则索引将从零重新开始。$nav = array_merge($Class->nav_top, $Class->nav_sub);
var_dump($nav);
# Output:
array(
0 => 'Current Sale',
1 => 'Retrieve Sale',
2 => 'Close Register',
.......
);
期望结果是数组键仍然相同。
# Expected Output
array(
100 => 'Dashboard',
200 => 'Sell',
300 => 'Products',
........
);
array_merge($a,$b)
,$b
中的键将覆盖$a
中匹配的键。而使用$a+$b
,$b
中的键不会覆盖$a
中匹配的键,但它们将被忽略。这在以下示例中有解释:https://www.php.net/manual/en/function.array-merge.php#example-5418 - Jules Colle