合并数组而没有丢失键索引 [英] Merge array without loss key index

查看:67
本文介绍了合并数组而没有丢失键索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组

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

如何组合这两个数组而不丢失关键索引?

How to combine this two array without losing it's key index?

如果我使用 array_merge()进行操作,索引将从零重新开始

if i do it with array_merge() the index will restart from zero

$nav = array_merge($Class->nav_top, $Class->nav_sub);
var_dump($nav);

# Output:
array(
    0 => 'Current Sale',
    1 => 'Retrieve Sale',
    2 => 'Close Register',
    .......
);

预期结果数组键仍然相同

expected result the array key still same

# Expected Output
array(
    100 => 'Dashboard',
    200 => 'Sell',
    300 => 'Products',
    ........
);


推荐答案

我能想到的最简单的方法:

The easiest I can think of:

$combined = $nav_top + $nav_sub;

一个例子

这篇关于合并数组而没有丢失键索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆