将多维数组中的值添加到另一个数组

时间:2018-01-03 15:51:44

标签: php arrays

大家好,无论你在哪里:D,我有一个问题,我有这个数组代码 $ arrayToView是我想要的每个用户的信息。 $ tagsArray只是使用每个用户的标签,但我需要合并所有信息,如最后一个数组......

$arrayToView = array(
    'IVOFACUNDO' = array(
         'mails' => 3,
         'contacts' => 34,
         'blocked' => 23
     ),
     'ESRAYCU' = array(
         'mails' => 23,
         'contacts' => 124,
         'blocked' => 44
     )
)

我有另一个这样的

$tagsArray= array(
    'IVOFACUNDO' = array(
         '14' => array(
             'id' => 14,
             'name' => 'php',
             'value' => 1
         ),
         '15' => array(
             'id' => 15,
             'name' => 'javascript',
             'value' => 1
         )
     ),
     'ESRAYCU' = array(
         '1' => array(
             'id' => 1,
             'name' => 'python',
             'value' => 1
         ),
         '15'=> array(
             'id' => 15,
             'name' => 'javascript',
             'value' => 1
         )
     )
)

所以问题是我如何能够明显地将这两个数组合并到相同的管理员这样的

$arrayToView = array(
    'IVOFACUNDO' = array(
         'mails' => 3,
         'contacts' => 34,
         'blocked' => 23,
         'tags' => array(
             '14' => array(
                 'id' => 14,
                 'name' => 'php',
                 'value' => 1
             ),
             '15' => array(
                 'id' => 15,
                 'name' => 'javascript',
                 'value' => 1
             )
         )
     ),
     'ESRAYCU' = array(
         'mails' => 23,
         'contacts' => 124,
         'blocked' => 44,
         'tags' => array(
             '1' => array(
                 'id' => 1,
                 'name' => 'python',
                 'value' => 1
             ),
             '15'=> array(
                 'id' => 15,
                 'name' => 'javascript',
                 'value' => 1
             )
         )
     )
)

需要在每次迭代的合并中创建关键'标签'以添加并获得一个包含所有值的数组,我该怎么做?

3 个答案:

答案 0 :(得分:0)

使用php内置函数

$result_Arr = array_merge_recursive($arrayToView,$tagsArray);

答案 1 :(得分:0)

您可以尝试使用此代码段。

foreach($arrayToView as $key => $arr){
   if(array_key_exists($key, $tagsArray)){
       $arrayToView[$key]['tags'] = $tagsArray[$key];
   }
}
echo '<pre>';print_r($arrayToView);echo '</pre>';

答案 2 :(得分:0)

<?php
$arrayToView = array(
    'IVOFACUNDO' => array(
         'mails' => 3,
         'contacts' => 34,
         'blocked' => 23
     ),
     'ESRAYCU' => array(
         'mails' => 23,
         'contacts' => 124,
         'blocked' => 44
     )
);

$tagsArray= array(
    'IVOFACUNDO' => array(
         '14' => array(
             'id' => 14,
             'name' => 'php',
             'value' => 1
         ),
         '15' => array(
             'id' => 15,
             'name' => 'javascript',
             'value' => 1
         )
     ),
     'ESRAYCU' => array(
         '1' => array(
             'id' => 1,
             'name' => 'python',
             'value' => 1
         ),
         '15'=> array(
             'id' => 15,
             'name' => 'javascript',
             'value' => 1
         )
     )
);

foreach($arrayToView as $key => $value){
    if(isset($tagsArray[$key])){
        $arrayToView[$key]['tags'] = array();
        foreach($tagsArray[$key] as $key2 => $value2){
            $arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
        }

    }   
}



echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>