Req #65202 [Opn->Nab]: array_rand not function
| From: | aharvey@php.net | Date: | Thu, 04 Jul 2013 19:39:15 +0000 |
| Subject: | Req #65202 [Opn->Nab]: array_rand not function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180076@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65202&edit=1
ID: 65202
Updated by: aharvey@php.net
Reported by: terracota09 at gmail dot com
Summary: array_rand not function
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Arrays related
Operating System: Windows 7
PHP Version: 5.3Git-2013-07-04 (Git)
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2013-07-04 17:11:04] 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 12:08:00] 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 10:03:17] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65202&edit=1