gpt4 book ai didi

php - 合并数组而不丢失键索引

转载 作者:行者123 更新时间:2023-12-02 22:59:46 26 4
gpt4 key购买 nike

我有两个数组

/**
* 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',
........
);

最佳答案

我能想到的最简单的:

$combined = $nav_top + $nav_sub;

An example .

关于php - 合并数组而不丢失键索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305912/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com