如何根据关键词的精确匹配重新排列数组的元素

时间:2014-07-01 16:34:01

标签: php mysql

是否可以根据条件或关键字重新排列数组的元素? 例如,将两个查询的结果放入和数组中,并根据关键词重新排列元素,然后在类似匹配之前先放置完全匹配。

$arr = array();

$sql = mysql_query("SELECT * FROM table WHERE (title LIKE '$key') OR (title LIKE '%$key') OR (title LIKE '%$key%') ORDER BY CASE WHEN title LIKE  '$key' THEN 0 ELSE 1 END DESC") or die (mysql_error());
while($data = mysql_fetch_array($sql)){
   $li = $data['title'];
   $arr[] = $li;
}

$sql2 = mysql_query("SELECT * FROM table WHERE (title LIKE '$key') OR (title LIKE '%$key') OR (title LIKE '%$key%') ORDER BY CASE WHEN title LIKE  '$key' THEN 0 ELSE 1 END DESC") or die (mysql_error());
while($data = mysql_fetch_array($sql2)){
   $li = $data['title'];
   $arr[] = $li;
}

// rearrange elements according to $key

0 个答案:

没有答案