Jump to content

[SOLVED] Array Shuffle issue


EchoFool

Recommended Posts

Hey

 

I'm having difficulties with my shuffle array function, its meant to shuffle the array then return it, and then when i echo position 0 in the array i get the value returned "1". Even thought its not in the array.. which is confusing...

 

Heres my script:

 

Function

<?php
function shuffledeck() { 
$cards = array('DA','D2','D3','D4','D5','D6','D7','D8','D9','D10','DJ','DQ','DK',
			'HA','H2','H3','H4','H5','H6','H7','H8','H8','H19','HJ','HQ','HK',
			'CA','C2','C3','C4','C5','C6','C7','C8','C9','C10','CJ','CQ','CK',
			'SA','S2','S3','S4','S5','S6','S7','S8','S9','S10','SJ','SQ','SK'); 
$Deck = shuffle($cards);
return array($Deck);
}
?>

 

On a php script:

<?php
$Deck = shuffledeck();
Echo $Deck[0];
?>

 

The Echo just displays 1

 

Where am i going wrong here?

Link to comment
https://forums.phpfreaks.com/topic/171574-solved-array-shuffle-issue/
Share on other sites

At some point, either before, during, and especially after what you are attempting does not work, you need to read the documentation for what you are doing. shuffle() does not return an array it shuffles the array it is given as a parameter -

 

Description

bool shuffle ( array &$array )

This function shuffles (randomizes the order of the elements in) an array.

 

Parameters

 

array

The array.

 

Return Values

Returns TRUE on success or FALSE on failure.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.