1

I have this code to insert data into an array called $usersEmails:

while($get = mysqli_fetch_assoc($query_run)){
    $userOne = $get['UserOne'];
    $userTwo = $get['UserTwo'];
    if($userOne != $id){
        array_push($usersEmails, $userOne);
    }else if($userTwo != $id){
        array_push($usersEmails, $userTwo);
    }
}

Then I have made a query using this array:

$query = 'SELECT `Email` FROM `users` 
          WHERE `Id` IN ('.implode(',', array_map('intval', $usersEmails)).')';

This code generates results ordered by users.Id, but I want these IDs to be ordered in the same order as they appear in my array.

In other words, I want the IDs to be output from 0 --> end of array

potashin
  • 44,205
  • 11
  • 83
  • 107

1 Answers1

1

Add to your sql query order by and use field:

$query = 'select `Email`
          from `users` 
          where `Id` in (' . implode(',', array_map('intval', $usersEmails)) . ')
          order by field(`Id`, ' . implode(',', array_map('intval', $usersEmails)) . ')
potashin
  • 44,205
  • 11
  • 83
  • 107