note 111165 deleted from function.rand by aharvey
| From: | aharvey@php.net | Date: | Tue, 22 Jan 2013 00:24:05 +0000 |
| Subject: | note 111165 deleted from function.rand by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193338@lists.php.net to get a copy of this message | ||
Note Submitter: thiagobluhm at hotmail dot com
----
I wrote this script to clear some aspects about array_rand, array_filter and callback function
array_filter's... It´s very simple, but maybe helps someone.
<?php
//Array 60 positions
$numbers = range(1, 60);
//Shuffle 60numbers
shuffle($numbers);
//Callback Function GET OUT zero with array_filter
function retiraZero($item) {Return ($item != 0);}
// More once time take rand numbers and Call retiraZero function
$orte = array_filter(array_rand($numbers, 6), "retiraZero");
// Glue Up array $orte
$numbersFinal = implode("-", $orte);
// ShowUp result
echo $numerosFinal;
?>
Output likes: "num1 - num2 - num3 - num4 - num5 - num6"
ps.: array_filter has a follow syntax : array_filter ( array $input [, callable $callback =
"" ] )... But this can bring a parser ERROR likes : syntax error, unexpected T_FUNCTION
in... because of this: i put Out callback function and calls it into this $orte =
array_filter(array_rand($numbers, 6), "retiraZero");