合并数组而不丢失密钥索引

Gus*_*ooL 13 php arrays

我有两个阵列

/**
 * 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'
);
Run Code Online (Sandbox Code Playgroud)

如何结合这两个数组而不失去它的关键索引?

如果我用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',
    .......
);
Run Code Online (Sandbox Code Playgroud)

预期结果数组键仍然相同

# Expected Output
array(
    100 => 'Dashboard',
    200 => 'Sell',
    300 => 'Products',
    ........
);
Run Code Online (Sandbox Code Playgroud)

jer*_*oen 48

我能想到的最容易:

$combined = $nav_top + $nav_sub;
Run Code Online (Sandbox Code Playgroud)

一个例子.

  • @Baba没有错误。真的:-) http://www.php.net/manual/zh/language.operators.array.php (2认同)