Php random replacement of elements in the logarithm group, php array element replacement _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php performs random replacement of elements in the logarithm group, and changes of php array elements. Php: the method for randomly changing elements in the logarithm group. The example in this article describes how to randomly change elements in the logarithm group of php. Share it with you for your reference. Specifically, php performs random replacement of elements in the logarithm group, and php array element replacement

This example describes how to randomly change elements in a php logarithm group. Share it with you for your reference. The specific analysis is as follows:

This is a user-defined function for random replacement of php Array elements. php already has a built-in shuffle ($ Array) function with the same functions. this code can be used as a reference.

// 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:

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

I hope this article will help you with php programming.

Examples in this article describes how php performs random replacement of elements in a logarithm group. Share it with you for your reference. Details...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.