/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] extract() woes

Okay,
I’ve been working on this for hours and I can get it close but just not there. I have the following code which randomizes a fixed array and returns 4 results.

[code=php]<?php
$cards = array(“ah”, “ac”, “ad”, “as”,
“2h”, “2c”, “2d”, “2s”,
“3h”, “3c”, “3d”, “3s”,
“4h”, “4c”, “4d”, “4s”,
“5h”, “5c”, “5d”, “5s”,
“6h”, “6c”, “6d”, “6s”,
“7h”, “7c”, “7d”, “7s”,
“8h”, “8c”, “8d”, “8s”,
“9h”, “9c”, “9d”, “9s”,
“th”, “tc”, “td”, “ts”,
“jh”, “jc”, “jd”, “js”,
“qh”, “qc”, “qd”, “qs”,
“kh”, “kc”, “kd”, “ks”);

srand(time());
for($i = 0; $i < 52; $i++)
{
$count = count($cards);
$random = (rand()%$count);
if($cards[$random] == “”)
{
$i–;
}
else
{
$deck[] = $cards[$random];
$cards[$random] = “”;
}
}
srand(time());
$starting_point = (rand()%51);
for ($index = 0; $index < 4; $index++)
{
if ($starting_point == 4) { $starting_point = 0; }
$shuf_deck = ($deck[$starting_point]);

$starting_point++;
}
?>[/code]

If I echo or print $shuf_deck I get a random 8 digit string along the lines of “2c9ckd3c”. This is mostly right. But what I need is to have the four pairs automatically assigned to variables. So for example, “2c9ckd3c” would be
$one = 2c;
$two = 9c;
etc.

I’ve been trying to use extract with no luck, but I need these 4 pairs of characters to later be inserted into different columns on a table.

Any help would really be appreciated.

Thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@antonioattOct 06.2011 — I think this is ok to complete your code:
[code=php]
<?php
$one='';
$two='';
$three='';
$four='';
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");

srand(time());
for($i = 0; $i < 52; $i++)
{
$count = count($cards);
$random = (rand()%$count);
if($cards[$random] == "")
{
$i--;
}
else
{
$deck[] = $cards[$random];
$cards[$random] = "";
}
}
srand(time());
$starting_point = (rand()%51);
for ($index = 0; $index < 4; $index++)
{
if ($starting_point == 4) { $starting_point = 0; }
$shuf_deck = ($deck[$starting_point]);
assignValue($one,$two,$three,$four,$shuf_deck);

$starting_point++;

}
function assignValue(&$one,&$two,&$three,&$four,$value){
if($one=='') {$one=$value;return;}
if($two=='') {$two=$value;return;}
if($three=='') {$three=$value;return;}
if($four=='') {$four=$value;return;}
}
echo "one=".$one."<br/>";
echo "two=".$two."<br/>";
echo "three=".$three."<br/>";
echo "four=".$four."<br/>";
?>
[/code]


I declared 4 var $one,$two,$three,$four with initial value '' and assigned their values with a function(called 4 times in the code):
[code=php] function assignValue(&$one,&$two,&$three,&$four,$value){...}[/code]
Notice reference for parameters &$one,&$two,&$three,&$four.


Hope this is what you triing to do

Bye
Copy linkTweet thisAlerts:
@criterion9Oct 06.2011 — I think this is ok to complete your code:
[code=php]
<?php
$one='';
$two='';
$three='';
$four='';
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");

srand(time());
for($i = 0; $i < 52; $i++)
{
$count = count($cards);
$random = (rand()%$count);
if($cards[$random] == "")
{
$i--;
}
else
{
$deck[] = $cards[$random];
$cards[$random] = "";
}
}
srand(time());
$starting_point = (rand()%51);
for ($index = 0; $index < 4; $index++)
{
if ($starting_point == 4) { $starting_point = 0; }
$shuf_deck = ($deck[$starting_point]);
assignValue($one,$two,$three,$four,$shuf_deck);

$starting_point++;

}
function assignValue(&$one,&$two,&$three,&$four,$value){
if($one=='') {$one=$value;return;}
if($two=='') {$two=$value;return;}
if($three=='') {$three=$value;return;}
if($four=='') {$four=$value;return;}
}
echo "one=".$one."<br/>";
echo "two=".$two."<br/>";
echo "three=".$three."<br/>";
echo "four=".$four."<br/>";
?>
[/code]


I declared 4 var $one,$two,$three,$four with initial value '' and assigned their values with a function(called 4 times in the code):
[code=php] function assignValue(&$one,&$two,&$three,&$four,$value){...}[/code]
Notice reference for parameters &$one,&$two,&$three,&$four.


Hope this is what you triing to do

Bye[/QUOTE]

This is way sloppy. I wouldn't use it.
Copy linkTweet thisAlerts:
@criterion9Oct 06.2011 — Okay,

I've been working on this for hours and I can get it close but just not there. I have the following code which randomizes a fixed array and returns 4 results.

[code=php]<?php
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");

srand(time());
for($i = 0; $i < 52; $i++)
{
$count = count($cards);
$random = (rand()%$count);
if($cards[$random] == "")
{
$i--;
}
else
{
$deck[] = $cards[$random];
$cards[$random] = "";
}
}
srand(time());
$starting_point = (rand()%51);
for ($index = 0; $index < 4; $index++)
{
if ($starting_point == 4) { $starting_point = 0; }
$shuf_deck = ($deck[$starting_point]);


$starting_point++;
}
?>[/code]


If I echo or print $shuf_deck I get a random 8 digit string along the lines of "2c9ckd3c". This is mostly right. But what I need is to have the four pairs automatically assigned to variables. So for example, "2c9ckd3c" would be

$one = 2c;

$two = 9c;

etc.

I've been trying to use extract with no luck, but I need these 4 pairs of characters to later be inserted into different columns on a table.

Any help would really be appreciated.

Thanks[/QUOTE]


Your "shuffling" portion can be done using shuffle() as it will handle creating the random seed and reordering the array so you don't have to worry about a random sort algorithm.

Might look something like:
[code=php]
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");
shuffle($cards);
[/code]


Then if you alter your "drawing" code a smidge:

for ($index = 0; $index < 4; $index++)

{

if ($starting_point == 4) { $starting_point = 0; }

$shuf_deck = ($deck[$starting_point]);


$starting_point++;

}
[/quote]

Becomes:
[code=php]
for ($index = 0; $index < 4; $index++)
{
echo $deck[$index];
}
[/code]


You have already "shuffled" the deck above. So there is no need for additional randomization.
Copy linkTweet thisAlerts:
@007JulienOct 06.2011 — It's not very usefull to calculate $count 52 time in the loop ! The method may be a little long to choose 40 cards...

An other way consists to choose cards among the remaining cards.
[code=php]<?php
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");

srand(time());
$cntCrd = count($cards); // Number of cards
$chsCrdArr = array();// An array of selected cards
$nbmChsCrd = 4;// Number of cards to choose

do {$rank = 1 + rand()%($cntCrd-($chsCrd=count($chsCrdArr)));// a rank among remaning cards
// we decrement $rank only with remaining cards and choose the card if the rank becomes null
for ($idx=0;;$idx++) if (!isset($chsCrdArr[$idx]) && (--$rank)==0) {$chsCrdArr[]=$cards[$idx];break;}}
while ($chsCrd!=$nbmChsCrd);

// An array with 4 random cards
print_r($chsCrdArr);
echo"<br>";
//
foreach ($chsCrdArr as $k=>$v) $cards[$v]="";
print_r($cards);
?>[/code]
Copy linkTweet thisAlerts:
@criterion9Oct 06.2011 — Your "shuffling" portion can be done using shuffle() as it will handle creating the random seed and reordering the array so you don't have to worry about a random sort algorithm.

Might look something like:
[code=php]
$cards = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");
shuffle($cards);
[/code]


Then if you alter your "drawing" code a smidge:

Becomes:
[code=php]
for ($index = 0; $index < 4; $index++)
{
echo $deck[$index];
}
[/code]


You have already "shuffled" the deck above. So there is no need for additional randomization.[/QUOTE]

Just to clarify...(and I saw a typo). The whole script could be something like:
[code=php]

$deck = array("ah", "ac", "ad", "as",
"2h", "2c", "2d", "2s",
"3h", "3c", "3d", "3s",
"4h", "4c", "4d", "4s",
"5h", "5c", "5d", "5s",
"6h", "6c", "6d", "6s",
"7h", "7c", "7d", "7s",
"8h", "8c", "8d", "8s",
"9h", "9c", "9d", "9s",
"th", "tc", "td", "ts",
"jh", "jc", "jd", "js",
"qh", "qc", "qd", "qs",
"kh", "kc", "kd", "ks");
shuffle($deck);

for ($index = 0; $index < 4; $index++)
{
echo $deck[$index];
}
[/code]
Copy linkTweet thisAlerts:
@wsparrowauthorOct 06.2011 — Okay. First of all, thanks to all of you for your suggestions. But it seems that my point got a little lost. The issue wasn't with randomizing the results. The issue was trying to get the results into a predetermined set of variables. So antonioatt was right. That is the result I was trying to get. I know there are 100 ways to shuffle, randomize, etc and I'm not opposed to suggestions on better ways to do it, but that wasn't where I was getting hung up.

Anyways, I've put together a bit of a hybrid between antonioatt's code and criterion's and it's doing the job nicely. So thanks again guys. Always a helpful bunch here.
×

Success!

Help @wsparrow spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...