本文介绍了在PHP中关联数组上的Array-Merge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在关联数组上执行array_merge?

How can i do an array_merge on an associative array, like so:

数组1:

$options = array (
"1567" => "test",
"1853" => "test1",
);

数组2:

$option = array (
"none" => "N/A"
);

所以我需要array_merge这两个,但是当我这样做时(在调试中):

So i need to array_merge these two but when i do i get this (in debug):

Array
(
    [none] => N/A
    [0] => test
    [1] => test1
)

推荐答案

尝试使用:

$ finalArray = $ options + $ option .请参见 http://codepad.org/BJ0HVtac 只需检查重复键的行为,我就没有对此进行测试.对于独特的键,它很棒.

$finalArray = $options + $option .see http://codepad.org/BJ0HVtacJust check the behaviour for duplicate keys, I did not test this. For unique keys, it works great.

<?php

$options = array (
                  "1567" => "test",
                  "1853" => "test1",
                  );


$option = array (
"none" => "N/A"
);


$final = array_merge($option,$options);

var_dump($final);


$finalNew = $option + $options ;

var_dump($finalNew);

?>

这篇关于在PHP中关联数组上的Array-Merge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:34