欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php對數(shù)組內(nèi)元素進行隨機調(diào)換的方法

 更新時間:2015年05月12日 09:13:38   作者:企鵝不笨  
這篇文章主要介紹了php對數(shù)組內(nèi)元素進行隨機調(diào)換的方法,通過自定義函數(shù)實現(xiàn)對數(shù)組內(nèi)元素進行隨機調(diào)換的功能,涉及php操作數(shù)組的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了php對數(shù)組內(nèi)元素進行隨機調(diào)換的方法。分享給大家供大家參考。具體分析如下:

這是一個自定義的php數(shù)組元素隨機調(diào)換的函數(shù),php已經(jīng)有一個內(nèi)置的同樣功能的函數(shù)shuffle($Array),這個代碼權(quán)當參考

// I noticed that there is already a built-in function that
// does the same - so don't use mine ;-)
//
// --> shuffle($Array);
//
// http://de2.php.net/manual/de/function.shuffle.php
//
function RandomizeArray($array){
  // error check:
  $array = (!is_array($array)) ? array($array) : $array;
  $a = array();
  $max = count($array) + 10;
  while(count($array) > 0){    
    $e = array_shift($array);
    $r = rand(0, $max);
    // find a empty key:
    while (isset($a[$r])){
      $r = rand(0, $max);
    }    
    $a[$r] = $e;
  }
  ksort($a);
  $a = array_values($a);
  return $a;
}

使用范例:

/*
** Example:
*/
$test_array = array('why','dont','visit','www','jonas','john','de',':-)');
print implode(", ", $test_array);
print "\n";
print implode(", ", RandomizeArray($test_array));
/*
Example output:
why, dont, visit, www, jonas, john, de, :-)
www, de, jonas, john, visit, why, :-), dont
*/

希望本文所述對大家的php程序設(shè)計有所幫助。

相關(guān)文章

最新評論