How can i merge multi arrays without using foreach

I am looking for a way to merge a multi arrays without using foreach () when doing so, i will prefer any built in array() and here is the code

// users
array(
      [0] => array(
       [userid] => 1,
       [email] => lewn@example.com,
       [phone] => 667790897
       ),
      [1] => array(
       [userid] => 2,
       [email] => hen@example.com,
       [phone] => 4477905534
       ),
);


// usersmeta
array(
      [56] => array(
       [userid] => 1,
       [city] => Atlanta,
       [state] => Georgia
       [country] => United States
       ),
      [79] => array(
       userid] => 2,
       [city] => Sheridan,
       [state] => Wyoming,
       [country] => United States
       ),
);

Now i want to merge this arrays where ever the userid of user and usermeta are same, and i don’t want to use foreach loop in archiving it, am looking at array_walk or other inbuilt array functions.

The expected results should look like this

// expected results
array(
      [0] => array(
       [userid] => 1,
       [email] => lewn@example.com,
       [phone] => 667790897,
       [city] => Atlanta,
       [state] => Georgia,
       [country] => United States
       ),
      [1] => array(
       userid] => 2,
       [email] => hen@example.com,
       [phone] => 4477905534
       [city] => Sheridan,
       [state] => Wyoming,
       [country] => United States
       ),
);

you would array_map. Which is doing a foreach, but without you typing the word ‘foreach’.

Note: This assumes you want to merge the same-numbered indicies in both arrays.

Yes i wan to merge the two but won’t follow the arrays keys because they are not same keys with usersmeta keys, however I wan to find anywhere userid is 1 in users array and also userid is 1 in usermeta

You could still array_map one of the arrays, and in the callback, array_filter the other to find the matching data.

I tried a couple of array_map but my call back function was returning nothing, please can you show me a walk around it?

Show me what you’ve done, and we’ll work from there.

Okay this is what i did.

$users = array(users); // array of users 
$usersmeta  = array(usermeta); // array of usermeta

function call_back($a, $b){
  $userid_user = $a['userid'];
  $userid_usermeta = $b['userid'];

if($userid_user == $userid_usermeta){
  // return the array keys
    $data = array_merge($a, $b);

   return $data;
}
}

$result = array_map('call_back', $users, $usermeta);

if am to use foreach loop i would have sort the array keys and use them to merge the two affected arrays like this $data = array_merge($a[$keya], $b[$keyb]);

You could do it this way →

$users = array(
    array(
        "userid" => 1,
        "email" => "lewn@example.com",
        "phone" => 667790897
    ),
    array(
        "userid" => 2,
        "email" => "hen@example.com",
        "phone" => 4477905534
    )
);

$usersmeta = array(
    56 => array(
        "userid" => 1,
        "city" => "Atlanta",
        "state" => "Georgia",
        "country" => "United States"
    ),
    79 => array(
        "userid" => 2,
        "city" => "Sheridan",
        "state" => "Wyoming",
        "country" => "United States"
    )
);

$result = array_merge_recursive($users, $usersmeta);

print_r($result);

For further info → https://www.php.net/manual/en/function.array-merge-recursive.php

i already tried it is not giving the desired results, what it does is to append the usermeta arrays below the entire user arrays

So I take it you want to keep the same indexes?

He’s not trying to ‘merge’ in the sense of appending, he’s trying to ‘merge’ in the sense of zipping. But his arrays dont have the same indexes.
(in SQL terms: He wants to OUTER JOIN the users array and the usersmeta array ON users.userid = usersmeta.userid)

Yes the indexes are not the same and using recursive won’t work

I don’t even care about the indexes, all I want is to have the exact user id from users array to be merged below the usersmeta array and be as one single array.

So i am expected to have two arrays with full datas properly merged.

in associative array can only have one value per key. Therefore, it’s not possible to have two keys with the same name.

yeah i know but in this case the indexes and keys don’t matter.

I can have something like this

// expected results
array(
      [780] => array(
       [userid] => 1,
       [email] => lewn@example.com,
       [phone] => 667790897,
       [city] => Atlanta,
       [state] => Georgia,
       [country] => United States
       ),
      [89] => array(
       userid] => 2,
       [email] => hen@example.com,
       [phone] => 4477905534
       [city] => Sheridan,
       [state] => Wyoming,
       [country] => United States
       ),
);

but when you look carefully you will notice that the correct value of city of the userid 2 is Sharidan and during our merging it properly came under the userid of 2

By default [userid] is overwritten with new values i am aware of it and we can’t have same key in a single array

exactly

Since that somewhat makes sense to me here’s something that might do the trick I think? (Well it looks right to me?)

<?php
$users = array(
    array(
        "userid" => 1,
        "email" => "lewn@example.com",
        "phone" => 667790897
    ),
    array(
        "userid" => 2,
        "email" => "hen@example.com",
        "phone" => 4477905534
    )
);

$usersmeta = array(
    56 => array(
        "userid" => 1,
        "city" => "Atlanta",
        "state" => "Georgia",
        "country" => "United States"
    ),
    79 => array(
        "userid" => 2,
        "city" => "Sheridan",
        "state" => "Wyoming",
        "country" => "United States"
    )
);

$result = array_map(function($user, $meta) {
    return array_merge($user, $meta);
}, $users, $usersmeta);

echo "<pre>" . print_r($result, 1) . "</pre>";


1 Like

Wow this is perfect, thanks alot but i need to know how the array_map() worked, it gave me the results I needed but I need to know how it did it, what and what was mapped?

Be careful this works only if the order of the user ids in both array are absolute the same

Really? So in real sense it was not actually looking for values of userid in both arrays then merge them using their keys regardless of which position the value was found?