-1

I have two array one array is fetched from database and second is the array of user id's mean i want to sort the fetched user array based on second given array. I want those users on the top of array who exists in second array in and those user whose not exists in second input array will also come after the sorted users in php

$sortArr = array(14,25);
$dbArr = array(7,5,100,25,110,78,14);
sort_tag_able_users($sortArr,$dbArr);

function sort_tag_able_users($inputs, $db_inputs) {
    foreach ($db_inputs as $url_response) {
        $key = array_search($url_response, $inputs);
        $result[$key] = $url_response;
    }
    ksort($result);
    return $result;
}

echo "Returned Reusult";
Array
(
    [0] => 14
    [1] => 25
)

echo "Required Reusult";
Array
(
    [0] => 14
    [1] => 25
    [2] => 7
    [3] => 5
    [4] => 100
    [5] => 110
    [6] => 78
)

above function that I have it only return the matched users and remaning users removed from the db array. Help to solve this issue.

Rizwan Saleem
  • 771
  • 5
  • 19
  • Can you paste examples of your arrays' structures and the expected result? – Paul Nov 08 '17 at 11:30
  • mention your both arrays in the question and what result you want to extract from these array – Vishal Solanki Nov 08 '17 at 11:30
  • Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/a/17364128/476) – the problem would be solved if you used a regular custom sorting callback; the problem is that `$key` is the same for all non-existing ids and hence you're overwriting them all. – deceze Nov 08 '17 at 11:32
  • @Bunker Boy I have mentioned both arrays and required result please review my question. Thank you – Rizwan Saleem Nov 08 '17 at 11:41

2 Answers2

1

The problem is that $key is going to be the same (false) for all ids not present in $inputs, so you're overwriting $result[false] many times, resulting in only the last value remaining.

Use a usort comparison callback instead:

public static function sort_tag_able_users($inputs, $db_inputs) {
    usort($db_inputs, function ($a, $b) use ($inputs) {
        $a = array_search($a->id, $inputs);
        $b = array_search($b->id, $inputs);

        if ($a === $b) {
            return 0;
        } else if ($a === false) {
            return 1;
        } else if ($b === false) {
            return -1;
        } else {
            return $a - $b;
        }
    });

    return $db_inputs;
}
deceze
  • 471,072
  • 76
  • 664
  • 811
0

You could change your code into something like this:

public static function sort_tag_able_users($inputs, $db_inputs) {
    foreach ($db_inputs as $url_response) {

        // assume that the user was not found
        $found = 0;

        $key = array_search($url_response->id, $inputs);
        if ($key != false) {
            // user was found
            $found = 1;
        }

        // group the users in two sub-arrays
        $users[$found][$url_response->id] = $url_response;
    }

    // append users that were not found after the users that were found
    $result = $users[1] + $users[0];
    return $result;
}

This way, all the users are preserved in the order you were looking for.

Zamfi
  • 326
  • 1
  • 9