I have a simple question (Not for me anyway:)):
I have the following 3 variables and I want to put them in a array and then getting the length of this array:
$rank1 = "1";
$rank2 = "2";
$rank3 = "3";
I am using this code (I have to use for!!!):
for($x = 1; $x <= 3; $x++) {
$array_rank .= "\"\$rank".$x."\", " ;
}
$array_rank2 = array($array_rank);
$array_rank_length = count($array_rank2);
The length of the array should be 3, I am getting 1.
Any help whould be appreciated.
Thanks in advance,
You are just adding a string to an array with the text '$rank1,$rank2,$rank3'. [insert image "that's not how any of this works"]
I believe what you are trying to achieve is something like this:
$rank1 = "1";
$rank2 = "2";
$rank3 = "3";
$array_rank = array();
for($x = 1; $x <= 3; $x++) {
$array_rank[$x] = ${'rank' . $x};
}
$array_rank_length = count($array_rank); //3
First correct '$rank1,$rank2,$rank3,' this (Because you are getting string)..
Then this code will help you..
<?php
$rank1 = "1";
$rank2 = "2";
$rank3 = "3";
$array_rank = '';
for($x = 1; $x <= 3; $x++) {
$array_rank.= "\"\$rank".$x."\", " ;
}
$array_rank2 = explode(',',$array_rank);
$array_rank_length = count($array_rank2)-1;
print_r($array_rank2);
print_r($array_rank_length);
?>
Related
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
here I am trying a random url with the variable $user and $mail with time intervals per 30 minutes.
<?php
$tmRand = srand(floor(time() /60*30));
$x = array('"site.com/$user"','"site.com/$mail"');
$y = $x[array_rand($x)];
?>
output "site.com/$user" or "site.com/$mail"
from the results of the above output I try to randomly use a loop by calling the variable $y in array $arr = array($y); but the results that come out in the loop "site.com/$user" not "site.com/username"
<?php
for ($i = 0; $i <= 5; $i++) {
$user = "username";
$mail = "email";
$arr = array($y);
$url = $arr[array_rand($arr)];
echo "$url\n";
sleep(2);
}
$i++;
I'm breaking this into multiple steps here. You might be able to condense it:
$x = []
$userElement = 'site.com/'.$user;
$mailElement = 'site.com/'.$mail;
array_push($x, $userElement);
array_push($x, $mailElement);
var_dump($x);
Im not sure why this isn't working, can anyone help? Im trying to see if
$lines[$y] is the same as "../../Files/Person/$username/files/$share_person_post" but even if it is, it isn't running through the code.
if (file_exists("../../Files/Person/$username/shared.txt")) {
$lines = file("../../Files/Person/$username/shared.txt");
$size = sizeof($lines);
$size = $size - 1;
$shared_location = "Unknown";
for($y = 0; $y <= $size; $y++) {
echo $lines[$y];
if ($lines[$y] == "../../Files/Person/$username/files/$share_person_post") {
echo "Here";
$z = $y + 1;
$shared_location = $lines[$z];
$fmsg = "<p>This file is already shared at $shared_location</p>";
$ex = "0";
}
}
}
Try using explode, I know for me when I grab text files, they are all one long string. It might be my server or php version, but its worth a shot.
if (file_exists("../../Files/Person/$username/shared.txt"))
{
$data = file("../../Files/Person/$username/shared.txt");
$lines = explode("\n",$data);
$size = count($lines);
$size = $size - 1;
$shared_location = "Unknown";
for($y = 0; $y <= $size; $y++)
{
echo $lines[$y];
if($lines[$y] == "../../Files/Person/$username/files/$share_person_post")
{
echo "Here";
$z = $y + 1;
$shared_location = $lines[$z];
$fmsg = "<p>This file is already shared at
$shared_location</p>";
$ex = "0";
}
}
}
PHP NOOB, i have been working on this code all day, but cant find any good way to fix it.
here is my code:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
$m.$i = 20;
}
else
{
$m.$i = 10;
}
}
Am trying to join $m.$i together and set it to 20 or 10, but i end up having me20 or me10 instead of me1 = 20 or me1 = 10 when i echo $m.$i which is legit, is there anyways to make this work?
$m.$i = 20;
This will assign $i = 20 and then concatenate it with $m and hence you will see me20.
What you need is $m . $i .= 20; instead. which will concatenate them altogether.
Fixed:
<?php
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
echo $m . $i .= 20;
}
else
{
echo $m.$i .= 10;
}
}
?>
EDIT:
The above answer was a total misunderstanding, I realised you intended to create the variables:
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
echo $me0;
}
else
{
${$m.$i} = 10;
}
}
Assign it like so.
${$m.$i} = 20;
You are trying to dynamically create variables, so you have to do something like this:
$priv = explode(',', '0,1,1');
$m = 'me';
for($i = 0; $i <= 2; $i++)
{
if($priv[$i] == '0')
{
${$m.$i} = 20;
}
else
{
${$m.$i} = 10;
}
}
then try to priny $me0, $me1
I am trying to deal a hand of five cards to a player and score them. My scoring program seems to be working fine, but I am running into the issue of duplicate cards getting dealt from time to time. I tried using a while loop to check for duplicate cards, but this seems kind of hackish. My code is below. Please keep in mind that I am definitely a neophyte, so the simpler the solution the better! Thanks so much.
// create suits array
$suits = array("996", "997", "998", "999");
// create faces array
$faces = array();
$faces[1] = "1";
$faces[2] = "2";
$faces[3] = "3";
$faces[4] = "4";
$faces[5] = "5";
$faces[6] = "6";
$faces[7] = "7";
$faces[8] = "8";
$faces[9] = "9";
$faces[10] = "10";
$faces[11] = "11";
$faces[12] = "12";
$faces[13] = "13";
// create player's hand
$card = array();
for ($i = 0; $i < 5; $i++)
{
$face_value = shuffle($faces);
$suit_value = shuffle($suits);
$card[$i] = $faces[$face_value].$suits[$suit_value];
$counter = 0;
while ($counter < 100)
{
if (in_array($card[$i], $card))
{
$face_value = shuffle($faces);
$suit_value = shuffle($suits);
$card[$i] = $faces[$face_value].$suits[$suit_value];
}
$counter++;
}
print ("<img src=\"../images/4/$card[$i].gif\">");
}
It might be more efficient to simply set up an array that has 52 elements, one for each of the cards.
$cards = range(0,51);
shuffle($cards);
$hand = array();
for ($i = 0; $i < 5; $i++)
{
$hand[$i] = $cards[$i];
}
Note that you can extract the suit and rank of a card $i simply, by doing
$suit = $hand[$i] % 4;
$rank = $hand[$i] / 4;
This will prevent duplicates.
EDIT: Suit and rank were reversed. They should be correct now.
Because you said you like it easy, you could create your arrays with range().
To avoid getting duplicate hands, check the $card array with before assigning the new hand.
the new code would look like:
// create suits array
$suits = range(996, 999);
// create faces array
$faces = range(0, 13);
// create player's hand
$card = array();
while ( count($card) < 5 )
{
$face_value = shuffle($faces);
$suit_value = shuffle($suits);
$newcard = $faces[$face_value].$suits[$suit_value];
if ( in_array($card, $newcard) ) {
$card[] = $newcard;
print ("<img src=\"../images/4/$newcard.gif\">");
}
}
I would definitely create a deck with all 52 cards in it like so:
// create suits array
$suits = range(996, 999);
// create entire deck
$deck = range(0, 51);
shuffle($deck);
// create player's hand
for ($i = 0; $i < 5; $i++) {
$suit_value = $suits[$deck[$i] % 4];
$face_value = floor($deck[$i] / 4) + 1;
print ("<img src=\"../images/4/{$face_value}{$suit_value}.gif\">");
}
How can I store this in a $_var variable ?
$s_number = 5;
$spn = '6';
echo "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++)
{
echo "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
echo "]";
Question is bit vague though it seems probably you are looking for this,
$string = "'Landscapes':[";
for ($i = 1; $i <= $s_number; $i++) {
$string .= "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$string .= "]";
echo $string;
Other suggestions that use strings are fine, but I prefer to create arrays for tasks like this:
$s_number = 5;
$spn = '6';
$landscapes_array = array();
for ($i = 1; $i <= $s_number; $i++) {
$landscapes_array[] = "'".$spn."/"."content"."/".$i.".png"."'".", ";
}
$landscapes = "'Landscapes':[" . implode('', $landscapes_array) . "]";
You could also try putting the for loop in a function passing your variables and then return the values to a new variable. That's like saving a loop in a variable.