2014-11-05 63 views
0

我得到這個數組:PHP array_rand多維價值

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

我嘗試了各種代碼,但我無法得到任何工作。

有什麼建議嗎?

謝謝!

+0

你嘗試過什麼碼? 「所有類型」很廣泛 – 2014-11-05 19:16:52

+0

你甚至不清楚你想要做什麼......起初我以爲你只是想從每個索引中隨機選擇一個值,但是在第二個數組中,「56」來自哪裏?請更清楚你想要完成什麼。 – 2014-11-05 19:22:39

+0

Adelphia有什麼意思,「56」來自哪裏?我需要的只是獲取該數組的一個隨機值? – 2014-11-05 19:27:05

回答

0

您可以使用一個遞歸函數

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

請注意,此功能不使用回報,我通過引用傳遞的陣列,它會修改傳遞的變量。

其餘的邏輯很簡單:我們遍歷數組,如果它是一個其他數組,然後我們調用該函數,導致它再次迭代。直到它是一個值,然後我們將val添加到由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函數計算長度...

1

foreach()函數內使用is_array()來檢查和循環內部數組。

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

$newarray[]變量包含您需要執行rand的所有元素。這應該可以解決你的問題。

0

試試這個...

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

你可以使用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]); 
} 
0

您可以使用簡單的遞歸迭代&簡單隨機價值迴歸類:

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