php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65202 array_rand not function
Submitted: 2013-07-04 10:03 UTC Modified: 2013-07-04 19:39 UTC
From: terracota09 at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.3Git-2013-07-04 (Git) OS: Windows 7
Private report: No CVE-ID: None
 [2013-07-04 10:03 UTC] terracota09 at gmail dot com
Description:
------------
http://localhost:8080/arrayAleatorio.php?juego=8

<?php
// 2 primeros numeros = nÂș juegos; 
$a = array(9, 26, 31, 42, 4, 2, 11, 13, 19, 20, 23, 24, 28, 30, 39, 39, 1, 5, 
14, 18, 36, 49, 15, 16, 25, 27, 33, 34, 38, 43, 48, 7, 10, 22, 32, 3, 6, 35, 44, 
47, 8, 12, 17, 29, 37, 45, 21, 46, 40, 41);

for ($j = 1; $j < $_GET["juego"]+1; $j++) {
	echo "<p style='font-family: Arial; font-size: 2em; text-align: center; 
color: blue'>";
	$rand_keys = array_rand($a, 6);
	asort($rand_keys);
	
	print_r($rand_keys);
	
	//$i = 0;
	for ($i = 0; $i < 6; $i++) {
	//foreach ($rand_keys as $val) {
		//$i++;
		if ($i < 5)
			echo $rand_keys[$i]." - "; //echo $val." - ";
		else
			echo $rand_keys[$i]; //echo $val;
	}
	echo "</p>";
	if (($j%2) == 0) 
		echo "<br/>";
	
}

Test script:
---------------
Array ( [0] => 4 [1] => 8 [2] => 13 [3] => 16 [4] => 17 [5] => 46 ) 4 - 8 - 13 - 16 - 17 - 46

Array ( [0] => 7 [1] => 9 [2] => 11 [3] => 16 [4] => 28 [5] => 32 ) 7 - 9 - 11 - 16 - 28 - 32


Array ( [0] => 18 [1] => 23 [2] => 29 [3] => 34 [4] => 35 [5] => 43 ) 18 - 23 - 29 - 34 - 35 - 43

Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 13 [4] => 27 [5] => 44 ) 1 - 6 - 7 - 13 - 27 - 44


Array ( [0] => 7 [1] => 14 [2] => 32 [3] => 41 [4] => 42 [5] => 48 ) 7 - 14 - 32 - 41 - 42 - 48

Array ( [0] => 7 [1] => 13 [2] => 15 [3] => 22 [4] => 27 [5] => 38 ) 7 - 13 - 15 - 22 - 27 - 38


Array ( [0] => 0 [1] => 2 [2] => 3 [3] => 29 [4] => 43 [5] => 49 ) 0 - 2 - 3 - 29 - 43 - 49

Array ( [0] => 18 [1] => 23 [2] => 33 [3] => 34 [4] => 36 [5] => 39 ) 18 - 23 - 33 - 34 - 36 - 39

Expected result:
----------------
0 not exist in array

Actual result:
--------------
Array ( [0] => 4 [1] => 8 [2] => 13 [3] => 16 [4] => 17 [5] => 46 ) 4 - 8 - 13 - 
16 - 17 - 46

Array ( [0] => 7 [1] => 9 [2] => 11 [3] => 16 [4] => 28 [5] => 32 ) 7 - 9 - 11 - 
16 - 28 - 32


Array ( [0] => 18 [1] => 23 [2] => 29 [3] => 34 [4] => 35 [5] => 43 ) 18 - 23 - 
29 - 34 - 35 - 43

Array ( [0] => 1 [1] => 6 [2] => 7 [3] => 13 [4] => 27 [5] => 44 ) 1 - 6 - 7 - 
13 - 27 - 44


Array ( [0] => 7 [1] => 14 [2] => 32 [3] => 41 [4] => 42 [5] => 48 ) 7 - 14 - 32 
- 41 - 42 - 48

Array ( [0] => 7 [1] => 13 [2] => 15 [3] => 22 [4] => 27 [5] => 38 ) 7 - 13 - 15 
- 22 - 27 - 38


Array ( [0] => 40 [1] => 2 [2] => 3 [3] => 29 [4] => 43 [5] => 49 ) 40 - 2 - 3 - 
29 - 43 - 49

Array ( [0] => 18 [1] => 23 [2] => 33 [3] => 34 [4] => 36 [5] => 39 ) 18 - 23 - 
33 - 34 - 36 - 39

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-04 12:08 UTC] anon at anon dot anon
But [0] does exist in array $a. It's the very first entry:
Array
(
    [0] => 9
    [1] => 26
    [2] => 31
    ...
}
That's why array_rand is generating it.

If you mean to generate values of $a rather than keys do:

for ($i = 0; $i < 6; $i++) {
	if ($i) echo " - ";
	echo $a[$rand_keys[$i]];
}
 [2013-07-04 17:11 UTC] terracota09 at gmail dot com
value 0 not exit.

Array ( [0] => 0 [1] => 2 [2] => 3 [3] => 29 [4] => 43 [5] => 49 ) 0 - 2 - 3 - 29 
- 43 - 49

Why exit it, here?
 [2013-07-04 19:39 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2013-07-04 19:39 UTC] aharvey@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 28 20:01:29 2024 UTC