FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Filling an array with random input doesn't quite work
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Filling an array with random input doesn't quite work [message #185266 is a reply to message #185265] Sat, 15 March 2014 22:14 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>> Daniel Pitts wrote:
>>> for ($x = 1; $x < 40; ++$x) {
>>> $ayr[$x] = rand(60, 69);
>>> }
>>>
>>> So much simpler, more efficient, and actually correct.
>>
>> No, this will produce an array of only 39 elements, assuming there was
>> not already one. This can be easily fixed, of course.
>>
>> Still,
>>
>> $keys = range(1, 40);
>> $ayr = array_combine($keys, array_map(function () {
>> return rand(60, 69);
>> }, $keys)));
>>
>> is much more elegant :)
>
> Yes, but it's a shame about the extra ')'.

Ah, yes, no idea how that got in there. Should have been

$keys = range(1, 40);
$ayr = array_combine($keys, array_map(function () {
return rand(60, 69);
}, $keys));

> I think
>
> $ayr = array_map(function () { return rand(60, 69); },
> array_fill(1, 40, null));
>
> is a little simpler.

ACK. Actually, I had experimented with array_fill() but, strangely enough,
I overlooked this possibility. (I had posted something similar with regard
to letters not long ago.)


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: readdir lists randomly
Next Topic: Nested PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 23:29:25 GMT 2024

Total time taken to generate the page: 0.05048 seconds