2 回答

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊
我建議這樣的解決方案:
$idquestionCollect = ["34 1", "7 2", "29 3", "29 4", "28 5", "27 6", "27 7", "28 8", "28 9", "34 10", "34 11", "34 12", "34 13"];
$results = [];
foreach ($idquestionCollect as $separateID) {
[$one, $two] = explode(" ", $separateID);
$results[] = [
"id" => rtrim($two),
"result" => rtrim($one),
];
}
usort($results, function($el1, $el2){
return $el1['result'] > $el2['result'];
});
$listIDs = '"' . implode('","', array_column($results, 'id'));
$query = "SELECT * FROM articles where id in($listIDs)";
...

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
創(chuàng)建一個(gè)scores索引數(shù)組id,然后對(duì)其進(jìn)行排序,然后提取鍵會(huì)更容易:
foreach ($idquestionCollect as $separateID) {
$parts = explode(" ", $separateID);
if ($parts[0] < 40) {
$listIDs[$parts[1]] = $parts[0];
}
}
asort($listIDs);
$listIDs = implode(',', array_keys($listIDs));
另一種娛樂方式:
preg_match_all('/(\d+) (\d+)/', implode('-', $idquestionCollect), $matches);
$listIDs = array_combine($matches[2], $matches[1]);
asort($listIDs);
$listIDs = implode(',', array_keys($listIDs));
對(duì)于您的數(shù)據(jù),兩者都會(huì)產(chǎn)生:
2,3,1
- 2 回答
- 0 關(guān)注
- 215 瀏覽
添加回答
舉報(bào)