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

问题描述 投票:0回答:3

你好早上好,无论你在哪里: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
             )
         )
     )
)

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

php arrays
3个回答
0
投票

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

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

0
投票

使用php内置函数

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

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>';
?>
© www.soinside.com 2019 - 2024. All rights reserved.