0

I am trying to merge arrays in php. Everything is coming from a database and two different results could look like this:

array[0]['id_user'] = 1
array[0]['tokenId'] = 123
array[1]['id_user'] = 2
array[1]['tokenId'] = 456
array[2]['id_user'] = 5
array[2]['tokenId'] = 789

array[0]['id_user'] = 1
array[0]['tokenId'] = 123
array[1]['id_user'] = 2
array[1]['tokenId'] = 752
array[2]['id_user'] = 3
array[2]['tokenId'] = 789

Using array_merge() and then array_unique() should give me something like this I expect:

unique_and_merged_array[0]['id_user'] = 1
unique_and_merged_array[0]['tokenId'] = 123
unique_and_merged_array[1]['id_user'] = 2
unique_and_merged_array[1]['tokenId'] = 456
unique_and_merged_array[2]['id_user'] = 5
unique_and_merged_array[2]['tokenId'] = 789
unique_and_merged_array[3]['id_user'] = 2
unique_and_merged_array[3]['tokenId'] = 752
unique_and_merged_array[4]['id_user'] = 3
unique_and_merged_array[4]['tokenId'] = 789

Then how can I sort by id_user value? I'm not sure to understand the different sorting php functions ...

2
  • You can try with the uasort function. Commented Oct 8, 2015 at 4:57
  • You can have the id_user value be the key of an associative array and have the value be an array of tokenId's Commented Oct 8, 2015 at 5:31

3 Answers 3

2

You can simply use usort like as

usort($unique_and_merged_array,function($a,$b){
   return $a['id_user'] - $b['id_user'];
});
print_r($result);

Output:

Array
(
    [0] => Array
        (
            [id_user] => 1
            [tokenId] => 123
        )

    [1] => Array
        (
            [id_user] => 2
            [tokenId] => 752
        )

    [2] => Array
        (
            [id_user] => 2
            [tokenId] => 456
        )

    [3] => Array
        (
            [id_user] => 3
            [tokenId] => 789
        )

    [4] => Array
        (
            [id_user] => 5
            [tokenId] => 789
        )

)

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

That's better. ;) Same as the other answer though except for an anonymous function. :)
As you can simply make it one liner then why to use anonymous function @Nerdwood
1

The PHP-based solution could look something like this:

//Define arrays for testing
$array1[0]['id_user'] = 1;
$array1[0]['tokenId'] = 123;
$array1[1]['id_user'] = 2;
$array1[1]['tokenId'] = 456;
$array1[2]['id_user'] = 5;
$array1[2]['tokenId'] = 789;

$array2[0]['id_user'] = 1;
$array2[0]['tokenId'] = 123;
$array2[1]['id_user'] = 2;
$array2[1]['tokenId'] = 752;
$array2[2]['id_user'] = 3;
$array2[2]['tokenId'] = 789;

//Define sort function - compares arrays using the 'id_user' value
function sortByUserId($array1, $array2)
{
    if ($array1['id_user'] == $array2['id_user']) {
        return 0;
    }
    return ($array1['id_user'] < $array2['id_user']) ? -1 : 1;
}

//Merge arrays
$unique_and_merged_array = array_merge($array1, $array2);

//Remove duplicate entries - note SORT_REGULAR in order to work with multidimensional arrays
$unique_and_merged_array = array_unique($unique_and_merged_array, SORT_REGULAR);

//Sort the arrays
uasort($unique_and_merged_array, 'sortByUserId');

//Enjoy  ;)
var_dump($unique_and_merged_array);

Comments

1
function cmp($a,$b){
         if ($a["id_user"] == $b["id_user"]) {
            return 0;
        }
        return ($a < $b) ? -1 : 1;
    }

    $unique_and_merged_array = array();
    $unique_and_merged_array[0]['id_user'] = 1;
    $unique_and_merged_array[0]['tokenId'] = 123;
    $unique_and_merged_array[1]['id_user'] = 2;
    $unique_and_merged_array[1]['tokenId'] = 456;
    $unique_and_merged_array[2]['id_user'] = 5;
    $unique_and_merged_array[2]['tokenId'] = 789;
    $unique_and_merged_array[3]['id_user'] = 2;
    $unique_and_merged_array[3]['tokenId'] = 752;
    $unique_and_merged_array[4]['id_user'] = 3;
    $unique_and_merged_array[4]['tokenId'] = 789;
    usort($unique_and_merged_array, "cmp");

1 Comment

you can use usort function as above

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.