如何将第一个数组的键映射到第二个数组php的键值

时间:2015-08-12 09:30:03

标签: php associative-array

我有2个数组 数组1

[display_options_checkbox] => Array
        (
            [body] => body
            [nid] => nid
            [title] => 0
            [created] => 0
            [changed] => 0
        )

数组2:

[midsvn] => Array
(
[mid_radio_body] => Desc
    [mid_radio_nid] => Assc
    [mid_radio_title] => Assc
    [mid_radio_created] => Assc
    [mid_radio_changed] => Assc
)

如何将display_options_checkbox的键映射到 midsvn 数组的键值?

例如: 我能获得这种阵列结构吗?

Array(
[body] => array(
[mid_radio_body] => Desc
)
[nid] => array(
[mid_radio_nid] => Assc
)

依旧......

Array(
[body] => Desc,
[nid] => Assc,
[title] => Assc,
[created] => Assc,
[changed] => Assc
)

2 个答案:

答案 0 :(得分:1)

第一个结果:

$result = array();
foreach (array_keys($array1['display_options_checkbox']) as $key) {
    $result[$key] = array('mid_radio_' . $key => $array2['midsvn']['mid_radio_' . $key]);
}

第二个结果:

$result = array();
foreach (array_keys($array1['display_options_checkbox']) as $key) {
    $result[$key] = $array2['midsvn']['mid_radio_' . $key];
}

答案 1 :(得分:0)

foreach($array1 as $key1 => $value1){
    if(array_key_exists($key1,$array2)){
        /* do your mapping stuff here */
    }
}