Home > Backend Development > PHP Tutorial > A simple example of php disrupting an array, a two-dimensional array, and a multi-dimensional array, disrupting a two-dimensional array_PHP tutorial

A simple example of php disrupting an array, a two-dimensional array, and a multi-dimensional array, disrupting a two-dimensional array_PHP tutorial

WBOY
Release: 2016-07-12 08:49:41
Original
954 people have browsed it

A simple example of php shuffling an array, a two-dimensional array, a multi-dimensional array, shuffling a two-dimensional array

The shuffle function in php can only shuffle a one-dimensional array, there are What's a quick and easy way to scramble a multidimensional array?
is provided in the manual

<&#63;php 
function shuffle_assoc($list) { 
 if (!is_array($list)) return $list; 
 $keys = array_keys($list); 
 shuffle($keys); 
 $random = array(); 
 foreach ($keys as $key) 
  $random[$key] = $list[$key]; 
 return $random; 
} 
&#63;>
Copy after login

The above is for two-dimensional arrays!

What is the following shuffle method for multi-dimensional arrays? As convenient and fast as possible :

<&#63;php 
function shuffle_assoc($list) { 
if (!is_array($list)) return $list; 

$keys = array_keys($list); 
shuffle($keys); 
$random = array(); 
foreach ($keys as $key) 
$random[$key] = shuffle_assoc($list[$key]); 

return $random; 
} 
&#63;>
Copy after login

The following function is also from the PHP manual, which can scramble multi-dimensional arrays:

/**
* Shuffles an associative array recursive
* 
* @param array $array
* @return array
*
*/

function rec_assoc_shuffle($array)
{
 $ary_keys = array_keys($array);
 $ary_values = array_values($array);
 shuffle($ary_values);
 foreach($ary_keys as $key => $value) {
  if (is_array($ary_values[$key]) AND $ary_values[$key] != NULL) {
   $ary_values[$key] = rec_assoc_shuffle($ary_values[$key]);
  }
  $new[$value] = $ary_values[$key];
 }
 return $new;
}
Copy after login

The above is the simple example of php disrupting an array, a two-dimensional array, and a multi-dimensional array brought to you by the editor. I hope you will support Bangkejia~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1136627.htmlTechArticlephp A simple example of disrupting a two-dimensional array and a multi-dimensional array, disrupting a two-dimensional array The shuffle function in php only If you can scramble a one-dimensional array, is there any way to quickly and easily scramble a multi-dimensional array? Manual...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template