如何重建没有重复和其他限制的阵列?(How to rebuild an array with no

2019-10-31 10:12发布

我有一个应用程序,使人们能够提供自定义的评估。 我已经建立在客户的请求,新的评分机制,允许参与者根据这对他们更准确的两个问题之间进行选择。 问题是,我需要随机的问题,确保来自同一类别的两个问题不一起出现,及限制它使来自同一两类两个问题相比仅三次。

我试图从这里其他问题适应代码/答案,但他们没有直接适用,我有难以适应这是最接近的人。

这里是我的含原题阵列的样品(已随机的,但没有其他标准)...

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. 确保类别相比仅三次

--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查询,拉不同对与类别多少次匹配的限制?

如果你知道一种方法通过数组来做到这一点,还是很想知道怎么做。 谢谢!

Answer 1:

以下是我终于想出了工作(尝试没有成功建立,我需要完成同样的事情后查询)...

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

最后,我遍历每个连击拉每个类别中的一对匹配的问题,并把结果保存在一个新的数组。 (I循环三次,因为每个类别配对将用于三次(问题对×3个=环60个问题的10个组合)。我也除去每个I从原来的拉问题$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']]);
    }
}

希望这可以帮助其他人!



文章来源: How to rebuild an array with no repeats & other limits?