0

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?

0

3 Answers 3

1

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>';
Sign up to request clarification or add additional context in comments.

2 Comments

Yo bro your answer works perfectly and the answer of @jack also!! thank you so much!!
great! it helps.. plz mark as answer. might someone also find it useful..
1

Use php inbuilt function

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

1 Comment

Yo bro your answer works perfectly and the answer of @Arbaz Khan also!! thank you so much!!
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>';
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.