Jump to content

Recommended Posts

Hey i'm having alot of trouble getting arrays working properly What i want to do is call a function called

generateDecks($numberofdecks) Then have it create that many decks - merge them into one array and shuffle them so they are random.

 

Then return the array. In the code below im just trying to print the array but it wont work. Also if i do return $decks

 

how do i call the function like this? $s = array(generateDecks(6));  ??

 

function generateDecks($noofdecks){
$decks = array();
for($i = 0; $i < $noofdecks; $i++){

//assign variables

$i = array( 
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts", 
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts", 
13 => "King of Hearts",    

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds", 
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds", 
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades", 
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades", 
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades", 
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades", 
52 => "King of Spades" );

array_merge($decks,$i);
}


print_r($decks);

 

 

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/
Share on other sites

you can use the double money sign....

function generateDecks($noofdecks){
$decks = array();
for($i = 0; $i < $noofdecks; $i++){

//assign variables

$$i = array( 
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts", 
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts", 
13 => "King of Hearts",    

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds", 
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds", 
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades", 
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades", 
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades", 
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades", 
52 => "King of Spades" );

array_merge($decks,$$i);
}


print_r($decks);

 

try it out...im not sure how well it will work....although i think this would fall under really bad practices...

 

Cheers! :-)

 

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838754
Share on other sites

try

function generateDecks($noofdecks){
$decks = array();
$tmp = array(
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts",
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts",
13 => "King of Hearts",   

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds",
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds",
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades",
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades",
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades",
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades",
52 => "King of Spades" );
for($i = 0; $i < $noofdecks; $i++){
array_merge($decks,$tmp);
}
print_r($decks);

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838942
Share on other sites

I tried the following but all that the print_r($decks) displays is

 

Array() 

 

What am i doing wrong?

 

<?PHP

//loop cards

function generateDecks($noofdecks){
$decks = array();


//assign variables

$deck = array( 
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts", 
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts", 
13 => "King of Hearts",    

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds", 
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds", 
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades", 
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades", 
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades", 
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades", 
52 => "King of Spades" );

for($i =0;$i<$noofdecks;$i++){
array_merge($decks,$deck);
}

//shuffle($decks);
print_r($decks);

}




?>

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839181
Share on other sites

http://myflexria.com/experiments/forumText.php

<?PHP

//loop cards

function generateDecks($noofdecks){
$decks = array();


//assign variables

$deck = array( 
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts", 
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts", 
13 => "King of Hearts",    

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds", 
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds", 
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades", 
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades", 
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades", 
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades", 
52 => "King of Spades" );

for($i = 0;$i<$noofdecks;$i++){
$decks = array_merge($decks,$deck); //assign the returned value of the function to the decks variable...
}

//shuffle($decks);
echo '<pre>';
print_r($decks);
echo '</pre>';
}

generateDecks(3);

 

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839266
Share on other sites

function generateDecks($noofdecks){
$decks = array();

//assign variables
$deck = array(
1 => "Ace of Hearts",
2 => "2 of Hearts",
3 => "3 of Hearts",
4 => "4 of Hearts",
5 => "5 of Hearts",
6 => "6 of Hearts",
7 => "7 of Hearts",
8 => "8 of Hearts",   
9 => "9 of Hearts",
10 => "10 of Hearts",
11 => "Jack of Hearts",
12 => "Oueen of Hearts",
13 => "King of Hearts",   

14 => "Ace of Diamonds",
15=> "2 of Diamonds",
16 => "3 of Diamonds",
17 => "4 of Diamonds",
18  => "5 of Diamonds",
19 => "6 of Diamonds",
20 => "7 of Diamonds",
21 => "8 of Diamonds",   
22 => "9 of Diamonds",
23 => "10 of Diamonds",
24 => "Jack of Diamonds",
25 => "Oueen of Diamonds",
26 => "King of Diamonds",   

27 => "Ace of Spades",
28=> "2 of Spades",
29 => "3 of Spades",
30 => "4 of Spades",
31  => "5 of Spades",
32 => "6 of Spades",
33 => "7 of Spades",
34 => "8 of Spades",   
35 => "9 of Spades",
36 => "10 of Spades",
37 => "Jack of Spades",
38 => "Oueen of Spades",
39 => "King of Spades",   

40 => "Ace of Spades",
41=> "2 of Spades",
42 => "3 of Spades",
43 => "4 of Spades",
44  => "5 of Spades",
45 => "6 of Spades",
46 => "7 of Spades",
47 => "8 of Spades",   
48 => "9 of Spades",
49 => "10 of Spades",
50 => "Jack of Spades",
51 => "Oueen of Spades",
52 => "King of Spades" );

for($i =0;$i<$noofdecks;$i++){
     $decks = array_merge($decks,$deck);
}
return $decks;
}

$three_decks = generateDecks(3);
var_dump($three_decks);

Link to comment
https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839267
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.