简体   繁体   中英

Add values from multidimensional array to another array

Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays $arrayToView is the info of every single user that i want. $tagsArray are only tags that use every user but i need to merge all the info something like the last array...

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

And i have another one like this

$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
         )
     )
)

so the question is how i can merge both arrays obviously respectively with the same admin something like this

$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
             )
         )
     )
)

The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?

使用php内置函数

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

You can try this snippet.

foreach($arrayToView as $key => $arr){
   if(array_key_exists($key, $tagsArray)){
       $arrayToView[$key]['tags'] = $tagsArray[$key];
   }
}
echo '<pre>';print_r($arrayToView);echo '</pre>';
<?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>';
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM