gpt4 book ai didi

php - 如何重建没有重复和其他限制的数组?

转载 作者:行者123 更新时间:2023-11-29 16:01:25 25 4
gpt4 key购买 nike

我有一个应用程序,允许人们提供自定义评估。我根据客户的要求建立了一种新的评分机制,允许参与者根据对他们来说更准确的方式在两个问题之间进行选择。问题是我需要随机化问题,确保同一类别中的两个问题不会一起出现,并对其进行限制,以便同一两个类别中的两个问题仅比较三次。

我尝试改编此处其他问题的代码/答案,但它们都没有直接适用,而且我很难改编最接近的代码/答案。

这是我的原始数组中的一个示例,其中包含问题(已经随机化,但没有其他标准)...

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 构建一个新数组...?

  1. 随机问题
  2. 将不同类别的问题配对
  3. 保险类别仅比较 3 次

--更新--添加 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;

--更新--

我将其作为 MySQL 问题发布,因为我相信构建正确的查询足以满足我的需要。如果您知道如何执行此操作,请发布到以下问题...

How do I build a MySQL query that pulls distinct pairs with a limit on how many times categories can match?

如果您知道通过数组实现此目的的方法,仍然很想知道如何做到这一点。谢谢!

最佳答案

这是我最终想出的有效方法(在尝试构建查询失败后,我需要完成同样的事情)...

原始数组 $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']]);
}
}

希望这对其他人有帮助!

关于php - 如何重建没有重复和其他限制的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com