turkman Posted May 20, 2009 Share Posted May 20, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/ Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 You're overwriting your for loop $i with an array. Change the name of the array. Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838550 Share on other sites More sharing options...
turkman Posted May 21, 2009 Author Share Posted May 21, 2009 i dont understand? The first array will be called 1 the second will be called 2 etc?? If i cant do that how can i get it to create a specific number of arrays and merge them? Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838729 Share on other sites More sharing options...
DarkSuperHero Posted May 21, 2009 Share Posted May 21, 2009 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! :-) Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838754 Share on other sites More sharing options...
turkman Posted May 21, 2009 Author Share Posted May 21, 2009 I can't try it out until i get home. However if it is considered bad practices. Could you recommend a good way of doing it? Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838936 Share on other sites More sharing options...
sasa Posted May 21, 2009 Share Posted May 21, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-838942 Share on other sites More sharing options...
turkman Posted May 21, 2009 Author Share Posted May 21, 2009 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839181 Share on other sites More sharing options...
turkman Posted May 21, 2009 Author Share Posted May 21, 2009 Anyone?? Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839253 Share on other sites More sharing options...
DarkSuperHero Posted May 21, 2009 Share Posted May 21, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839266 Share on other sites More sharing options...
Ken2k7 Posted May 21, 2009 Share Posted May 21, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839267 Share on other sites More sharing options...
turkman Posted May 21, 2009 Author Share Posted May 21, 2009 excellent. Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/158969-help-with-arrays/#findComment-839286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.