how to call variable in php array - php

<?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);

Related

Call an existing php variable by concatenating two strings

I have these seven php variables:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
results = array('A','B','C');
I would like to loop through the results array and pass the corresponding variables to a function.
foreach($results as $value) {
$data = '$data'.$value;
$target = '$target'.$value;
buildOutput('$data'.$value,'$target'.$value);
}
// example, first time thru, wish to pass $dataA variable and $targetA variable
function buildOutput($data,$target) {
echo "data=$data,target=$target<br>"; // echo's strings "$dataA" and "$targetA"
}
I cannot figure out how to declare the variables. Or if this is even possible.
I have more than just $data and $target variables, but I simplified down to two for the question.
I would not recommend using it, but try this:
$dataA = 1;
$targetA = 2;
$dataB = 3;
$targetB = 4;
$dataC = 5;
$targetC = 6;
$results = array('A','B','C');
foreach($results as $value) {
buildOutput(${'data'.$value}, ${'target'.$value});
}
function buildOutput($data, $target) {
echo "data=$data,target=$target<br>";
}

Execute an echo or class x times in php

I'm having the problem that i want to echo the class $password as often, as the number of $intcount is. Can you give me any idea how I would do that?
f.e. that I echo 4 random words if $intcount is 4, 5 words if its 5 etc etc.
<?php
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
$rand = mt_rand( $min , $max );
$out = $csv[$rand][$rand];
$password = $out;
// var_dump($count);
// var_dump($intcount);
//
// echo $count . "\n <br>";
// echo $password * $intcount;
//
// for ($i=0; $i < $intcount ; $i++) {
// echo $password;
// }
?>
The commented code doesn't need to stay necessarily.
Thanks :)
You need to create the $rand inside the loop like:
$csv = array_map('str_getcsv', file('gen.csv'));
$count = $_GET['count'];
$intcount = (int)$count;
$min = 0;
$max = 4;
for ($i=0; $i < $intcount ; $i++)
{
$rand = mt_rand($min, $max);
$password = $csv[$rand][$rand];
echo $password;
}

The length of the array is wrong

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);
?>

php merging or concatenation strings when assigning a variable

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

How to create array from variable php using loop for?

How to create array from variable php using loop for ?
i have many variable php like
$number_0 = 1;
$number_1 = 2;
$number_2 = 5;
$number_3 = 2;
$number_4 = 6;
i want to create array like this
$ar = array('1','2','5','2','6');
but using loop for like
for ($i=0;$i<5;$i++)
{
$number."_".$i ====> to array
}
Not a recomended way of doing things but:
$arr = array();
for($i=0;$i<5;$i++) {
$varName = 'number_'.$i;
$arr[] = $$varName;
}
for ($i = 1; $i <= 6; $i++)
$ar[] = $i;
for ($i=1;$i<7;$i++)
{
$ar[] = $i;
}

Categories