如何重建无重复的数组其他限制? [英] How to rebuild an array with no repeats & other limits?

查看:51
本文介绍了如何重建无重复的数组其他限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

总共有30个问题。为了确保分布均匀,两个类别的问题应该只进行三次比较。

There are 30 questions total. To insure there is an even distribution, questions from the two categories should only be compared three times.

如何使用PHP that从这个数组中构建一个新数组。 。?

How do I build a new array from this one using PHP that...?


  1. 随机化问题

  2. 在不同类别之间配对问题

  3. 只比较保险类别三次

-UPDATE--
添加MySQL表结构以防万一

--UPDATE-- Adding MySQL table structure in case it's easier to build an advanced query to do what I'm looking for...

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-

I我将其发布为MySQL问题,因为我相信建立正确的查询就可以满足我的需求。如果您有关于如何执行操作的想法,请发布到以下问题...

I'm posting this as a MySQL question as I believe building the right query will suffice for what I'm needing. If you have an idea on how to do so, please post to the following question...

我如何构建一个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)...

原始数组 $ theresults 包含来自5个不同类别的所有60个问题。我首先构建所有问题类别的数组...

The original array $theresults contains all 60 questions from the 5 different categories. 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);

最后,我遍历每个组合以提出与配对中的每个类别匹配的问题并存储结果放在一个新数组中。 (我将其循环3次,因为每个类别配对将被使用3次(问题对的10个组合x 3个循环= 60个问题。)我还从原始的 $ theresults 数组以确保没有重复...

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

// 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!

这篇关于如何重建无重复的数组其他限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆