简体   繁体   English

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

[英]How to rebuild an array with no repeats & other limits?

I have an app that allows people to serve custom assessments. 我有一个应用程序,允许人们提供自定义评估。 I've built a new scoring mechanism at a client's request that allows the participant to select between two questions according to which is more accurate for them. 我根据客户的要求建立了一个新的评分机制,允许参与者根据哪个更准确地选择两个问题。 The problem is that I need to randomize the questions, insure that two questions from the same category do not appear together, & limit it so two questions from the same two categories are only compared three times. 问题是我需要随机化问题,确保来自同一类别的两个问题不会一起出现,并限制它,因此来自相同两个类别的两个问题仅进行三次比较。

I've tried to adapt code/answers from other questions here, but none of them directly apply and I'm having difficulty adapting the ones that are the closest. 我试图在这里修改其他问题的代码/答案,但没有一个直接适用,我很难适应最接近的问题。

Here is a sample from my original array containing the questions (already randomized, but without the other criteria)... 这是我原始数组中包含问题的样本(已经随机化,但没有其他标准)......

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
        )
)

There are 30 questions total. 总共有30个问题。 To insure there is an even distribution, questions from the two categories should only be compared three times. 为了确保均匀分布,两个类别的问题只应进行三次比较。

How do I build a new array from this one using PHP that...? 如何使用PHP构建一个新数组...?

  1. Randomizes the questions 随机化问题
  2. Pairs questions between different categories 对不同类别之间的问题
  3. Insures categories are only compared three times 保险类别仅进行三次比较

--UPDATE-- Adding MySQL table structure in case it's easier to build an advanced query to do what I'm looking for... --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-- --UPDATE--

I'm posting this as a MySQL question as I believe building the right query will suffice for what I'm needing. 我将此作为MySQL问题发布,因为我相信构建正确的查询就足以满足我的需求。 If you have an idea on how to do so, please post to the following question... 如果您对如何操作有所了解,请发布以下问题...

How do I build a MySQL query that pulls distinct pairs with a limit on how many times categories can match? 如何构建一个MySQL查询来提取不同的对,并限制类别可以匹配的次数?

If you know a way to accomplish this via arrays, would still love to know how to do so. 如果您知道通过数组实现此目的的方法,仍然会想知道如何这样做。 Thanks! 谢谢!

Here is what I finally came up with that worked (after attempting unsuccessfully to build the query I needed to accomplish the same thing)... 这是我最终提出的工作(尝试构建查询失败后我需要完成相同的事情)...

The original array $theresults contains all 60 questions from the 5 different categories. 原始数组$theresults包含来自5个不同类别的所有60个问题。 I begin by building an array of all the question categories... 我首先构建一个包含所有问题类别的数组......

// 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;
    }
}

Then I use the following function to pull all the unique combinations of categories... 然后我使用以下函数来拉出所有类别的独特组合......

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);

Lastly, I loop through each of the combos to pull questions that match each category in the pair and store the result in a new array. 最后,我遍历每个组合以提取与该对中的每个类别匹配的问题,并将结果存储在新数组中。 (I loop it three times because each category pairing will be used three times (10 combinations of question pairs x 3 loops = 60 questions.) I also remove each question I pull from the original $theresults array to insure there are no duplicates... (我循环三次,因为每个类别配对将使用三次(问题对的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']]);
    }
}

Hopefully this helps someone else! 希望这有助于其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM