【问题标题】:php array_rand multidimensional valuephp array_rand 多维值
【发布时间】:2014-11-05 19:13:48
【问题描述】:

我得到了这个数组:

**Array 1 Example:**
Array
        (
            [0] => 62
            [1] => 9
            [2] => Array
                (
                    [0] => 5
                    [1] => 16
                    [2] => 45
                )

            [3] => Array
                (
                    [0] => 11
                    [1] => 21
                    [2] => 25
                    [3] => 32
                    [4] => 50
                )

            [4] => Array
                (
                    [0] => 4
                    [1] => 23
                    [2] => 37
                    [3] => 57
                )

            [5] => Array
                (
                    [0] => 13
                    [1] => 15
                    [2] => 18
                    [3] => 22
                    [4] => 27
                    [5] => 30
                )

        )

我的问题是,我无法从#Array 1 示例中获取随机值。所以我试图在一个新数组中收集所有值,所以它看起来像#Array 2 Example,但对我来说制作这个数组似乎几乎是不可能的。

**Array 2 Example:**

Array
        (
            [0] => 62
            [1] => 56
            [2] => 16
            [3] => 44
            [4] => 54
            [5] => 11
            [6] => 21
            [7] => 25
            [8] => 32
            [9] => 33
            [10] => 4
            [11] => 12
            [12] => 23
            [13] => 57
            [14] => 13
            [15] => 15
            [16] => 18
            [17] => 22
            [18] => 27
            [19] => 30
        )

我尝试了所有类型的代码,但我无法使任何工作。

有什么建议吗?

谢谢!

【问题讨论】:

  • 你试过什么代码? “各种”是广义的
  • 甚至不清楚你要做什么......起初我以为你只是想从每个索引中获取一个随机值,但后来,56 在第二个数组中来自哪里?请更清楚您要完成的工作。
  • Adelphia 有什么意义,“56”从何而来?我需要的只是取该数组的随机值?

标签: php arrays random multidimensional-array


【解决方案1】:

foreach() 函数中使用is_array() 来检查和循环内部数组。

foreach($array as $piece) {
    if(is_array($piece){
        foreach($piece as $item)
            $newarray[] = $item;
    } else {
        $newarray[] = $piece;
    }
}

$newarray[] 变量包含执行 rand 所需的所有元素。这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    试试这个...

    $flatarray = array();
    foreach($orig_array as $ra)
    {
       if ( is_array($ra) )
          $flatarray = array_merge($flatarray,$ra);
       else   
          array_push($flatarray,$ra);
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用递归函数

      function compress_arr($arr, &$new_arr){
          foreach($arr as $val){
              if(is_array($val)){
                  compress_arr($val, $new_arr);
              } else {
                  $new_arr[] = $val;
              }
          }
      }
      $arr_n = array();
      compress_arr($array, $arr_n);
      

      注意这个函数不使用return,因为我通过引用传递数组,它会修改传递的变量。

      其余的逻辑很简单:我们迭代数组,如果它是另一个数组,那么我们调用函数,使其再次迭代。直到它是一个值,然后我们将 val 添加到 ref 传递的新数组中。这个函数将把所有结构为嵌套数组的东西压缩成一个单数组,不管子层数的大小。

      如果你不喜欢通过 ref 传递,你可以修改成这样:

      function compress_arr($arr){
          $arr_ret = array();
          foreach($arr as $val){
              if(is_array($val)){
                  array_merge($arr_ret, compress_arr($val));
              } else {
                  $arr_ret[] = $val;
              }
          }
          return $arr_ret;
      }
      

      现在要在该数组中取一个随机值,只需使用array_rand function。它会让你用 rand 函数计算长度......

      【讨论】:

        【解决方案4】:

        您可以使用RecursiveIteratorIteratorRecursiveArrayIteratoriterator_to_array

        您的代码将如下所示:

        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));
        $newArray  = iterator_to_array($iterator, false);
        var_dump($newArray);
        

        它给了我这个结果:

        array(20) {
          [0]=>
          int(62)
          [1]=>
          int(9)
          [2]=>
          int(5)
          [3]=>
          int(16)
          [4]=>
          int(45)
          [5]=>
          int(11)
          [6]=>
          int(21)
          [7]=>
          int(25)
          [8]=>
          int(32)
          [9]=>
          int(50)
          [10]=>
          int(4)
          [11]=>
          int(23)
          [12]=>
          int(37)
          [13]=>
          int(57)
          [14]=>
          int(13)
          [15]=>
          int(15)
          [16]=>
          int(18)
          [17]=>
          int(22)
          [18]=>
          int(27)
          [19]=>
          int(30)
        }
        

        我们现在可以像这样从数组中选择一个或多个随机值:

        $items = array_rand($newArray, 10);
        var_dump($items);
        

        这会给我们这 10 个键:

        array(10) {
          [0]=>
          int(1)
          [1]=>
          int(2)
          [2]=>
          int(3)
          [3]=>
          int(4)
          [4]=>
          int(5)
          [5]=>
          int(7)
          [6]=>
          int(9)
          [7]=>
          int(13)
          [8]=>
          int(14)
          [9]=>
          int(15)
        }
        

        然后我们可以遍历这些键并获取这些值:

        int(9)
        int(5)
        int(16)
        int(45)
        int(11)
        int(25)
        int(50)
        int(57)
        int(13)
        int(15)
        

        所以最后你可以使用这个代码:

        $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($vals));
        $newArray = iterator_to_array($iterator, false);
        $keys     = array_rand($newArray, 10);
        
        foreach($keys as $key){
            var_dump($newArray[$key]);
        }
        

        【讨论】:

          【解决方案5】:

          您可以使用简单的递归迭代器和简单的随机值返回类:

              class Randomizer
                  {
                      public  static $new;
          
                      // Recursive iterator that stores array values in the $new variable
                      public  static  function Combine($array = array())  {
                              foreach($array as $key => $value) {
                                      if(!is_array($value)) {
                                              self::$new[]    =   $value;
                                          }
                                      else
                                          self::Combine($value);
                                  }
          
                              return self::$new;      
                          }
          
                      // Returns a randomized value
                      public  static  function Fetch($arr)
                          {
                              // Depending on needs, you could easily
                              // add shuffle, or other array functions
                              // otherwise you can just skip this method/function
                              $val    =   array_rand($arr);
                              return $val;
                          }
                  }
          
          // If you had more advanced functions in the Fetch() class use this way
          $test   =   Randomizer::Fetch(Randomizer::Combine($array));
          
          // Or simply use
          $test   =   array_rand(Randomizer::Combine($array));
          
          print_r($test);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-03-20
            • 1970-01-01
            • 2014-11-06
            • 1970-01-01
            • 2016-09-11
            • 1970-01-01
            • 2013-05-06
            • 2013-06-12
            相关资源
            最近更新 更多