dongzipu7517 2012-04-24 21:00
浏览 14
已采纳

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

i have two array

/**
 * 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?

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',
    ........
);
  • 写回答

2条回答 默认 最新

  • duanren9163 2012-04-24 21:02
    关注

    The easiest I can think of:

    $combined = $nav_top + $nav_sub;
    

    An example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件