8/8 Show what you know


#1





Hi Guys,

Could you help me out. I understand the excersise except of the ; $winnaar = $famname[rand(0, count($famname) - 1)]; - part.

At first; the [] thing - how should I supposed to know? And how does it work?
[rand(0, count($array) -1]

rand = select random. Between ()
what does the count commando do
where is the -1 for?

.


// Create an array and push on the names
    // of your closest family and friends
       $famname = array();
        array_push($famname, "Abbie");
        array_push($famname, "Rob");
        array_push($famname, "Mike");
        array_push($famname, "Zoe");
        array_push($famname, "Alan");
        array_push($famname, "Pedro");
        array_push($famname, "Tiago");
	// Sort the list
        sort($famname);        
	// Randomly select a winner!
        $winnaar = $famname[rand(0, count($famname) - 1)];        

	// Print the winner's name in ALL CAPS
	    print strtoupper($winnaar);


#2

count function in PHP returns the total length of an array...suppose you have an array

$myArray = ['sam' , 'billy' , 'mark' , 'jack' , 'john' ];


count($myArray)

will return 5 as the length of the array is 5

and
count ($myArray ) - 1

will be 5 - 1 = 4....that's what - 1 denotes

hope that helps...


#3

Thanks Abhi,

Some other notes;

$myArray = ['sam' , 'billy' , 'mark' , 'jack' , 'john' ]
echo count ($myArray) = 5

Q1: Why is it neccacery in the excerise to first sort to array?
Q2: In the case count ($myArray) -1 = minus John (unsorted) or Sam (sorted), correct?
Q3: Why is the minus 1 neccacery in the rand function. Is there also a other way like : $randArray = rand $myArray
Q4: [ ] is equal to ( ) , correct ? In that case the $myArray [rand(0, count($myArray) -1] = equal to $myArray (rand(0, count($myArray) -1) , correct?


#4

NO....count has nothing to do with sorting ...you see here we first sort the array and then select a winaar
This line

$winnaar = $famname[rand(0, count($famname) - 1)];

count($famname) - 1); will return 6

then

so the overall expression becomes

famname [rand( 0, 6 )];

At index 0 we have abbie
At index 1 we have Rob
At index 2 we have Mike
At index 3 we have Zoe
At index 4 we have Alan
At index 5 we have Pedro
At index 6 we have Tiago

So the
$winaar
will be
rand( 0 , 6 )
i.e an element between index 0 and index 6 (which is the last element)

Hope it's clear to you...


#5

Hi Abhi,

$famname = $myArray // echo count($myArray) = 7

but rand does not work with 7 array-positions. The positions are 0 t/m 6. There for you alsways need to put -1 in there. The 7th array is the 6th postion.

Still some other questions;
Q1: Why is it neccacery in the excerise to first sort to array?
Q2: Then do you use: [ ] and when ( ) ?


#6

Hi koen.vdb,

Answer to question 1 ... Sorting arrays makes things clear to distinguish..just like in real life we sort things or sometimes money.
Here is a link that describes what kinds of sort methods are available in php.

http://www.w3schools.com/php/php_arrays_sort.asp

Answer to question 2... We can define array in php in two ways

way 1

$myArray = ['someValue' , 'someValue' , 'someValue'];

way 2

$myArray = array('someValue' , 'someValue' , 'someValue');

Both does exactly the same thing. Don't get confused. I prefer using the first syntax, it's easier to write.

And don't mix things at your end with $myArray , I just gave an example creating $myArray.

Is everything clear now? Feel free to ask ....


#7

$memb = array();
    array_push ($memb, "Inna");
    array_push ($memb, "Serge");
    array_push ($memb, "Andrew");
    array_push ($memb, "Hleb");
    array_push ($memb, "Tomas");
    array_push ($memb, "Maksim");
    array_push ($memb, "Sveta");
    array_push ($memb, "Dinara");
    // Sort the list
    sort($memb);
	// Randomly select a winner!
    $roulette = rand (0, count($memb) - 1);
    
	// Print the winner's name in ALL CAPS
	$res = strtoupper($memb [$roulette]);
	print $res;

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.