【问题标题】:PHP check if an array contains a specific value every tot elementsPHP检查数组是否包含每个tot元素的特定值
【发布时间】:2013-07-25 19:52:13
【问题描述】:

我有一个长数组,如下所示。 我需要实现的是检查该数组中的每 6 个元素是否找到值 1。如果未找到,则必须随机添加到 6 个元素之一(通过替换任何原始值)。此外,如果每 6 个元素发现该值超过 2 次,则需要将其中一个替换为另一个随机数。

我已经创建了能够随机生成以下数组的代码。 但是我不知道如何检查数组中每6个元素是否包含值1,如果不是如何随机添加,如果发现超过1次如何将其中一个值替换为另一个随机数。

我知道这很难理解。我希望有人能够提供帮助。

简而言之,最终结果应该是:

此数组每 6 个元素必须至少包含一次且不超过一次的值 1,每 6 个元素位于随机位置。

这是我到目前为止所做的,这段代码生成了下面的数组:

/*
We select the elements to add randomly in the final array from a DB table
*/
    $elements = parent::$db->select('_matrix_elements', 'element_id', NULL, 'ORDER BY RAND()');
    $count = count($elements) - 1; //count the total number of the elements -1 to start from zero

    $columns = 6;
    $lines = 8;

    //generating the final array
    for ($a = 1; $a <= $lines; $a++) {
        for ($b = 1; $b <= $columns; $b++) {
            $rand = rand(1,$count);
            $line['position_'.$a.'_' . $b] = $elements[$rand]['element_id'];
        }
    }

Array
(
    [position_1_1] => 14
    [position_1_2] => 6
    [position_1_3] => 5
    [position_1_4] => 6
    [position_1_5] => 9
    [position_1_6] => 8
    [position_2_1] => 11
    [position_2_2] => 7
    [position_2_3] => 6
    [position_2_4] => 1
    [position_2_5] => 7
    [position_2_6] => 5
    [position_3_1] => 14
    [position_3_2] => 5
    [position_3_3] => 4
    [position_3_4] => 7
    [position_3_5] => 4
    [position_3_6] => 10
    [position_4_1] => 6
    [position_4_2] => 2
    [position_4_3] => 2
    [position_4_4] => 1
    [position_4_5] => 7
    [position_4_6] => 6
    [position_5_1] => 3
    [position_5_2] => 7
    [position_5_3] => 8
    [position_5_4] => 10
    [position_5_5] => 3
    [position_5_6] => 2
    [position_6_1] => 8
    [position_6_2] => 2
    [position_6_3] => 10
    [position_6_4] => 2
    [position_6_5] => 10
    [position_6_6] => 9
    [position_7_1] => 6
    [position_7_2] => 10
    [position_7_3] => 4
    [position_7_4] => 8
    [position_7_5] => 1
    [position_7_6] => 5
    [position_8_1] => 2
    [position_8_2] => 7
    [position_8_3] => 4
    [position_8_4] => 7
    [position_8_5] => 9
    [position_8_6] => 13
)

【问题讨论】:

  • 你考虑过二维数组吗? $position[7][4] = 8?
  • @MadaraUchiha 我不明白它有什么帮助
  • 不确定它是否真的可以帮助解决最初的原始问题。但是用嵌套数组代替编号名称,以后会解决很多问题。
  • @DiegoPucci - 你的描述有点不清楚,但我想我明白了。您要做的是设置一个数组,其中每个元素本身就是 6 个数字的元素;这是您要检查的那些子阵列。对吗?
  • @andrewsi 没有数组是最终的。我只需要随机添加值 1 任何 6 个键。所以从 p_1_1 和 p_1_6 一个值必须是 1,从 p_2_1 到 p_2_6 也是如此,以此类推

标签: php arrays random


【解决方案1】:
  1. 将列表拆分为一个二维数组,每个一维数组有 6 个元素
  2. 统计每个元素在6元素子数组中出现的次数
  3. 如果数组中没有元素,则做一个随机元素1
  4. 如果数组中有 2 个或更多
  5. 用一个随机数(不是一个)替换,直到只有一个
  6. 将最终的子数组添加到一维数组中
  7. 对二维数组中的所有子数组重复
$list_2d = array_chunk($list, 6); 
$final_list = array();
foreach ($list_2d as $array) { 
    $count = array_count_values($array);
    if (!array_key_exists(1, $count))
        $array[mt_rand(0, 5)] = 1;
    else if ($count[1] > 1)
        for ($i = 1; $i <= $count[1] - 1; $i++)
            $array[array_search(1, $array)] = mt_rand(2, 15);
            //Use whatever random number you want here, as long as it's not 1
    $final_list = array_merge($final_list, $array);
}

【讨论】:

    【解决方案2】:

    最好马上得到它,而不是为修复它制定另一个解决方案。用你的代码试试这两个函数。我也对其进行了测试,因此请在此处查看http://phpfiddle.org/lite/code/i9e-gb3。希望这是你需要的。

    function randomizer($columns, $rows, $elements)
    {
        $line = array();
        for ($row = 1; $row <= $rows; $row++)
        {
            $one_count = array();
    
            for ($col = 1; $col <= $columns; $col++)
            {
                $line['position_' . $row . '_' . $col] = randomID($elements);
    
                if ($line['position_' . $row . '_' . $col] == 1)
                {
                    //we have 1 - store key value
                    $one_count[] = 'position_' . $row . '_' . $col;
                }
            }
    
            if (empty($one_count))
            {
                //no 1 in last row - we will replace one random
                $rand_col = rand(1, $columns);
                $line['position_' . $row . '_' . $rand_col] = 1;
            }
            elseif (count($one_count) > 1)
            {
                //more than one 1 in last row, we will leave only one
    
                //first - pick one random to keep
                $keep_key = array_rand($one_count);
                unset($one_count[$keep_key]);
    
                // second - replace others with non 1 values
                foreach ($one_count as $repl_key)
                {
                    //this time we won't take ID=1
                    $line[$repl_key] = randomID($elements, true);
                }
            }
        }
    
        return $line;
    }
    
    
    function randomID($elements, $not1=false)
    {
        $count = count($elements) - 1;
    
        $rand = rand(0, $count);
        $el_id = $elements[$rand]['element_id'];
    
        if ($not1 === true && $el_id == 1)
        {
            return randomID($elements, true);
        }
    
        return $el_id;
    }
    

    差点忘了,关于这个$rand = rand(0, $count);$count = count($elements) - 1; 并从1 开始 rand :)

    【讨论】:

      猜你喜欢
      • 2016-07-02
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 2012-02-11
      相关资源
      最近更新 更多