我得到所有由數據庫以逗號分隔的關鍵字並從它們創建數組,然後合併所有數組。從結果數組中,我只保留獨特的項目array_unique
。問題是我無法按值排序結果數組。無法按值排列數組項目ASC
這是我的代碼:
$select_all_keywords = mysqli_query($db_connect, "SELECT `keywords` FROM `bookmarks`") or die(mysqli_error());
$keywords_array = array();
while($keywords = mysqli_fetch_assoc($select_all_keywords))
{
$explode_keywords = explode(", ", $keywords['keywords']);
$keywords_array = array_merge($keywords_array, $explode_keywords);
}
$unique_keywords = array_unique($keywords_array);
sort($unique_keywords);
$unique_keywords = array_values($unique_keywords);
print_array($unique_keywords);
的印刷陣列:
array(23) {
[0]=>
string(10) "Awolnation"
[1]=>
string(7) "Belgium"
[2]=>
string(7) "Gravity"
[3]=>
string(21) "Nervo (Musical Group)"
[4]=>
string(5) "R3..."
[5]=>
string(22) "R3hab (Musical Artist)"
[6]=>
string(5) "Remix"
[7]=>
string(4) "Sail"
[8]=>
string(30) "Tomorrowland (Recurring Event)"
[9]=>
string(21) "Tomorrowland Festival"
[10]=>
string(9) "Unlimited"
[11]=>
string(6) "dasdas"
[12]=>
string(10) "freshbooks"
[13]=>
string(11) "gdsfgdsfgds"
[14]=>
string(6) "mockup"
[15]=>
string(3) "php"
[16]=>
string(11) "programming"
[17]=>
string(10) "revolution"
[18]=>
string(17) "tomorrowland 2013"
[19]=>
string(11) "ummet ozcan"
[20]=>
string(10) "web design"
[21]=>
string(7) "wikihow"
[22]=>
string(3) "xml"
}
我tryed幾乎所有排序數組函數。他們都沒有幫我按值ASC排序陣列。
Oooof,對於一對多的關係,你不應該把它們存儲在一張表中。 – Terry
爲什麼'$ unique_keywords = array_values($ unique_keywords);'? – geomagas
@geomagas我也需要這些關鍵字的自動完成腳本,我需要'json_encode'數組。如果沒有「array_values」,數組不僅會顯示數值,還會顯示按鍵,而我只需要自動完成的值。 – sorinu26