要按数组中的顺序对结果进行排序,您可以执行以下操作:
$array_of_ordered_ids = array(4,5,2,6);
As you already know the order of the numbers, you can use the Mysql FIELD()Docs http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field function:
ORDER BY FIELD(id, 4, 5, 2, 6);
To create such a string, you can use implodeDocs http://php.net/implode:
$order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids));
试一试:
$array_of_ordered_ids = array(4,5,2,6);
$this->db->where_in('id', $array_of_ordered_ids);
$order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids));
$this->db->order_by($order);