[英]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.