<?php
bool shuffle ( arr &$array )
where,
&$array = The input ARRAY
?>
<?php
$arr01s = ["x", "Z", "a", "y", "B", "k"];
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
if(shuffle($arr01s) == true)
{
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
}
?>
<?php
$startval02 = 4;
$endval02 = 23;
$step02 = 3;
$arr02 = range($startval02, $endval02, $step02);
foreach($arr02 as $a02 => $ar02)
{
echo '{' . $a02 . '] => ' . $ar02 . '<br>';
}
echo '<br><br>';
@shuffle($arr02);
foreach($arr02 as $a02 => $ar02)
{
echo '{' . $a02 . '] => ' . $ar02 . '<br>';
}
?>
<?php
$arr03 = [ 2, 8, '18', 32, 'THIRTY-TWO', "eighteen", 8 ];
foreach($arr03 as $a03 => $ar03)
{
echo '{' . $a03 . '] => ' . $ar03 . '<br>';
}
echo '<br><br>';
@shuffle($arr03);
foreach($arr03 as $a03 => $ar03)
{
echo '{' . $a03 . '] => ' . $ar03 . '<br>';
}
?>
<?php
/*
* Test behaviour of shuffle when an array with default keys
* is passed to the 'array_arg' argument and check for the
* changes in the input array by printing the input array
* before and after this function is applied on it
*/
echo "With arrays having default keys:<br><br>";
// Initialise the array with integers
$array_arg_int = [ 0, 10, 20, 30, 40, 50, 60, 70, 80 ];
// Initialise the array with strings
$array_arg_strings = [ "one", 'two', 'three',
"four", "five", " ", 'six', ' ', "seven" ];
/* Testing this function with array of integers */
// printing the input array with integers
// before the shuffle operation
echo "<br><br>Input array of integers before<br>
this function is applied:<br>";
var_dump( $array_arg_int );
// applying this function on the input array of integers
echo "<br><br>Return value from this function:<br>";
var_dump( shuffle($array_arg_int) );
// prints the return value from this function
echo "<br><br>Resultant array after<br>
this function is applied:<br>";
var_dump( $array_arg_int );
/* Testing this function with array of strings */
// printing the input array with strings
// before the shuffle operation
echo "<br><br>Input array of strings before<br>
this function is applied:<br>";
var_dump( $array_arg_strings );
// applying this function on the input array of strings
echo "<br><br>Return value from this function:<br>";
var_dump( shuffle($array_arg_strings) );
// prints the return value from this function
echo "<br><br>Resultant array after<br>
this function is applied:<br>";
var_dump( $array_arg_strings );
?>
<?php
/*
* Test behaviour of shuffle when an associative array is
* passed to the 'array_arg' argument and check for the
* changes in the input array by printing the input array
* before and after this function is applied on it
*/
echo "With associative array:<br><br>";
// Initialise the associative array
$array_arg = array(
'one' => 1, 2 => 02, 'three' => 3,
4 => 4, '#5' => 5, 'SIX' => 6,
"seven" => 0x7, "#8" => 012, "nine" => 9
);
// printing the input array before the shuffle operation
echo "<br>Input array before this function is applied:<br>";
var_dump( $array_arg );
// applying this function on the input array
echo "<br><br>Return value from this function:<br>";
var_dump( shuffle($array_arg) );
// prints the return value from this function
echo "<br><br>Resultant array after this function is applied:<br>";
var_dump( $array_arg );
?>
<?php
/*
* Test behaviour of this function
* when multi-dimensional array is
* passed to 'array_arg' argument
*/
echo "With multi-dimensional array:<br>";
// initialise the multi-dimensional array
$array_arg = array(
array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9),
array(10000, 20000000, 30000000),
array(0, 0, 0),
array(012, 023, 034),
array(0x1, 0x0, 0xa)
);
// calling this function with
// multi-dimensional array
var_dump( shuffle($array_arg) );
echo "<br>The output array is:<br>";
var_dump( $array_arg );
// looping to test this with
// each sub-array
// in the multi-dimensional array
echo "<br><br>With arrays having different types of values:<br>";
$counter = 1;
for($i=0; $i<=6; $i++) {
echo "<br><br>Iteration: $counter<br>";
var_dump( shuffle($array_arg[$i]) );
echo "<br><br>The output array is:<br>";
var_dump( $array_arg[$i] );
$counter++;
}
?>
<?php
/*
* Test behaviour of shuffle() function
* when arrays having different
* types of values,
* are passed to 'array_arg' argument
*/
// initialise different arrays
$array_arg = array(
// array with positive int values
/*1*/ array(0, 1, 2, 2147483647 ),
// array with negative int values
array(-1, -2, -2147483647 ),
// array with positive float values
/*3*/ array(0.23, 1.34, 0e2, 200e-2, 30e2,
10e0, 2147473648.90),
// array with negative float values
array(-0.23, -1.34, -200e-2,
-30e2, -10e0, -2147473649.80),
// array with single quoted and double quoted strings
/*5*/ array('one', "123numbers", 'hello\tworld',
"hello world\0", '12.34floatnum'),
// array with bool values
array(true, TRUE, FALSE, false),
// array with positive hexa values
/*7*/ array(0x123, 0xabc, 0xABC, 0xac, 0xAb1, 0x9fa),
// array with negative hexa values
array(-0x123, -0xabc, -0xABC, -0xAb1, -0x9fa),
// array with positive octal values
/*9*/ array(0123, 0234, 034, 00),
// array with negative octal values
/*10*/ array(-0123, -0234, -034),
);
// looping to test shuffle() with each
// sub-array in the $array_arg array
echo "<br><br>With arrays having different types of values:<br>";
$counter = 1;
foreach($array_arg as $arr) {
echo "<br><br>Iteration: $counter<br>";
var_dump( shuffle($arr) );
echo "<br>The output array is:<br>";
var_dump( $arr );
$counter++;
}
?>
<?php
/*
* Test behaviour of shuffle() function
* when associative arrays
* having different types of values,
* are passed to 'array_arg' argument
*/
// initialise different arrays
$array_arg = array(
// array with positive int values
/*1*/ array("zero" => 0, 1 => 1, "two" => 2,
"max_int" => 2147483647 ),
// array with negative int values
array("minus_one" => -1, 'minus_two' => -2,
"min_int" => -2147483647 ),
// array with positive float values
/*3*/ array("float1" => 0.23, 'float2' => 1.34,
"exp1" => 0e2, 'exp2' => 200e-2, "exp3" => 10e0),
// array with negative float values
array(-0.23 => -0.23, -1.34 => -1.34,
-200e-2 => -200e-2, -30 => -30e0, -2147473649.80),
// array with single and double quoted strings
/*5*/ array('1' => 'one', "str1" => "123numbers",
'' => 'hello\tworld',
"" => "hello world\0", "12.34floatnum"),
// array with bool values
array('1' => TRUE, "1" => TRUE, "0" => FALSE, '0' => FALSE),
// array with positive hexa values
/*7*/ array("hex1" => 0x123, 'hex2' => 0xabc,
"hex\t3" => 0xABC, "hex\04" => 0xAb1),
// array with negative hexa values
array(NULL => -0x123, "NULL" => -0xabc,
"-ABC" => -0xABC, -0xAB1 => -0xAb1),
// array with positive octal values
/*9*/ array(0123 => 0123, "0234" => 0234,
'034' => 034, 00 => 00),
// array with negative octal values
array(-0123 => -0123, "-0234" => -0234, '-034' => -034),
// array with null values
/*11*/ array(NULL => NULL, "null" => NULL, "NULL" => NULL)
);
// looping to test shuffle() with
// each sub-array in the $array_arg array
echo "<br><br>With arrays having<br>
different types of values:";
$counter = 1;
foreach($array_arg as $arr) {
echo "<br><br>Iteration: $counter<br>";
var_dump( shuffle($arr) );
echo "<br>The output array is:<br>";
var_dump( $arr );
$counter++;
}
?>
<?php
/*
* Test behaviour of shuffle()
* when an array of heredoc strings
* is passed to 'array_arg' argument of the function
*/
echo "With array containing heredoc strings:<br><br>";
// defining different heredoc strings
$empty_heredoc = <<<EOT
EOT;
$heredoc_with_newline = <<<EOT
\n
EOT;
$heredoc_with_characters = <<<EOT
first line of heredoc string
second line of heredoc string
third line of heredocstring
EOT;
$heredoc_with_newline_and_tabs = <<<EOT
hello\tworld\nhello\nworld\n
EOT;
$heredoc_with_alphanumerics = <<<EOT
hello123world456
1234hello\t1234
EOT;
$heredoc_with_embedded_nulls = <<<EOT
hello\0world\0hello
\0hello\0
EOT;
// defining array with values as heredoc strings
$heredoc_array = array(
$empty_heredoc,
$heredoc_with_newline,
$heredoc_with_characters,
$heredoc_with_newline_and_tabs,
$heredoc_with_alphanumerics,
$heredoc_with_embedded_nulls
);
// defining array with keys as heredoc strings
$heredoc_asso_array = array(
$empty_heredoc => "heredoc1",
$heredoc_with_newline => "heredoc2",
$heredoc_with_characters => "heredoc3",
$heredoc_with_newline_and_tabs => "heredoc3",
$heredoc_with_alphanumerics => "heredoc4",
$heredoc_with_embedded_nulls => "heredoc5"
);
// test shuffle() with array
// containing heredoc strings as values
echo "With array of heredoc strings:<br>";
var_dump( shuffle($heredoc_array) );
echo "<br>The output array is:<br>";
var_dump( $heredoc_array );
// test shuffle() with array
// containing heredoc strings as its keys
echo "<br><br>With array having
heredoc strings as keys:<br>";
var_dump( shuffle($heredoc_asso_array) );
echo "<br>The output array is:<br>";
var_dump( $heredoc_asso_array );
?>