[英]How to rebuild an array with no repeats & other limits?
我有一個應用程序,允許人們提供自定義評估。 我根據客戶的要求建立了一個新的評分機制,允許參與者根據哪個更准確地選擇兩個問題。 問題是我需要隨機化問題,確保來自同一類別的兩個問題不會一起出現,並限制它,因此來自相同兩個類別的兩個問題僅進行三次比較。
我試圖在這里修改其他問題的代碼/答案,但沒有一個直接適用,我很難適應最接近的問題。
這是我原始數組中包含問題的樣本(已經隨機化,但沒有其他標准)......
Array
(
[0] => Array
(
[question_id] => 2087
[category_id] => 287
[question] => Question would appear here
)
[1] => Array
(
[question_id] => 2068
[category_id] => 286
[question] => Question would appear here
)
[2] => Array
(
[question_id] => 2067
[category_id] => 286
[question] => Question would appear here
)
[3] => Array
(
[question_id] => 2073
[category_id] => 286
[question] => Question would appear here
)
[4] => Array
[question_id] => 2029
[category_id] => 283
[question] => Question would appear here
)
[5] => Array
(
[question_id] => 2083
[category_id] => 287
[question] => Question would appear here
)
[6] => Array
(
[question_id] => 2084
[category_id] => 287
[question] => Question would appear here
)
[7] => Array
(
[question_id] => 2036
[category_id] => 283
[question] => Question would appear here
)
[8] => Array
(
[question_id] => 2062
[category_id] => 285
[question] => Question would appear here
)
[9] => Array
(
[question_id] => 2045
[category_id] => 284
[question] => Question would appear here
)
[10] => Array
(
[question_id] => 2052
[category_id] => 285
[question] => Question would appear here
)
)
總共有30個問題。 為了確保均勻分布,兩個類別的問題只應進行三次比較。
如何使用PHP構建一個新數組...?
--UPDATE--添加MySQL表結構,以防更容易構建高級查詢來執行我正在尋找的...
CREATE TABLE `questions` (
`question_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(5) unsigned NOT NULL,
`question` text NOT NULL,
PRIMARY KEY (`question_id`),
UNIQUE KEY `question_id` (`question_id`),
KEY `questions_ibfk_1` (`category_id`),
CONSTRAINT `questions_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`category_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
CREATE TABLE `categories` (
`category_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`category` varchar(64) NOT NULL,
PRIMARY KEY (`category_id`),
UNIQUE KEY `category_id` (`category_id`),
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
--UPDATE--
我將此作為MySQL問題發布,因為我相信構建正確的查詢就足以滿足我的需求。 如果您對如何操作有所了解,請發布以下問題...
如何構建一個MySQL查詢來提取不同的對,並限制類別可以匹配的次數?
如果您知道通過數組實現此目的的方法,仍然會想知道如何這樣做。 謝謝!
這是我最終提出的工作(嘗試構建查詢失敗后我需要完成相同的事情)...
原始數組$theresults
包含來自5個不同類別的所有60個問題。 我首先構建一個包含所有問題類別的數組......
// Create array of all categories
$allcategories = array();
$this->db->select('category_id');
$this->db->where('template_id',$template_id);
$query_thecategories = $this->db->get('categories');
$number_thecategories = $query_thecategories->num_rows();
if ($number_thecategories>0) {
foreach ($query_thecategories->result() as $row_thecategory) {
$thecategory = 'cat_' . $row_thecategory->category_id;
$$thecategory = '0';
$allcategories[] = $row_thecategory->category_id;
}
}
然后我使用以下函數來拉出所有類別的獨特組合......
function array_search_by_key($array, $key, $value) {
if(!is_array($array)) {
return [];
}
$results = [];
foreach($array as $element) {
if(isset($element[$key]) && $element[$key] == $value) {
$results[] = $element;
}
}
return $results;
}
$uniquecombos = uniquecombos($allcategories, 2);
最后,我遍歷每個組合以提取與該對中的每個類別匹配的問題,並將結果存儲在新數組中。 (我循環三次,因為每個類別配對將使用三次(問題對的10個組合x 3個循環= 60個問題。)我還刪除了我從原始$theresults
數組中提取的每個問題,以確保沒有重復... 。
// Create an empty array to capture the paired questions
$pairs = array();
// Loop through unique combos array 3 times to build pairings
for($combos = 1; $combos <= 3; $combos++) {
foreach ($uniquecombos as $theset) {
// Get two categories in pair
$firstcategory = $theset[0];
$secondcategory = $theset[1];
// Gather other arrays which matches each category
$matchesfirst = array_search_by_key($theresults,'category_id',$firstcategory);
shuffle($matchesfirst);
$matchessecond = array_search_by_key($theresults,'category_id',$secondcategory);
shuffle($matchessecond);
// Get question from each new array & add; remove selected question from the original array
$pairs[] = $matchesfirst[0];
unset($theresults[$matchesfirst[0]['question_id']]);
$pairs[] = $matchessecond[0];
unset($theresults[$matchessecond[0]['question_id']]);
}
}
希望這有助於其他人!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.