Ivo Facundo
Ivo Facundo

Reputation: 125

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?

Upvotes: 0

Views: 49

Answers (3)

Rodrigo NBK
Rodrigo NBK

Reputation: 1

<?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>';
?>

Upvotes: 0

jack
jack

Reputation: 609

Use php inbuilt function

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

Upvotes: 1

Arbaz Khan
Arbaz Khan

Reputation: 80

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>';

Upvotes: 1

Related Questions