Case Looping in PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to use a loop to iterate through my array, calling my function to print out all of these messages. I have to somehow keep track - I'm using PHP code.
This my code :
<? php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
echo $total = $rp-$v; ?>
my output should be:
10000
9000
7500
7000
5000
1000

Is this what you want?
<?php
$count = 6;
$rp = 11000;
$amount = array(1000,1000,1500,500,2000,4000);
foreach ($amount as $v) {
$rp = $rp - $v;
$count--;
//echo $count;
echo $rp."<br/>" ;
}
?>

Related

Division PHP in foreach [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have
$div = 100
$value = 502
$sum = $val / $div
how can i get output like this
100
100
100
100
100
2
any reffrence to learn more?
Here's one way to do it:
$div = 100;
$value = 502;
while($value > $div) {
$value = $value - $div;
echo $div . "<br>";
}
echo $value

a sum of some values in an php array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have this array`
<?php
$arr = [['name'=>'john','age'=>20,'sex'=>'m'],
['name'=>'maria','age'=>12,'sex'=>'f'],
['name'=>'nick','age'=>25,'sex'=>'m'],
['name'=>'jo','age'=>31,'sex'=>'f'],];
foreach ($arr as $persoana) {
foreach ($persoana as $id=>$value) {
if ($id == 'age') {
$sumvarsta = $sumvarsta + $value;
$n++;
}
}
}`?>
i need total average age (total average age seems to be working ok) , average age for women and average age for men.
How to calculate ?
thx.
<?php
$arr = [['name'=>'john','age'=>20,'sex'=>'m'],
['name'=>'maria','age'=>12,'sex'=>'f'],
['name'=>'nick','age'=>25,'sex'=>'m'],
['name'=>'jo','age'=>31,'sex'=>'f'],];
$womansum = 0;
$womancount = 0;
$mansum = 0;
$mancount = 0;
foreach ($arr as $persoana) {
if ($persoana['sex'] == 'm')
{
$mansum += $persoana['age'];
$mancount++;
} else {
$womansum += $persoana['age'];
$womancount++;
}
}
$manAverage = $mansum / $mancount;
$womanAverage = $womansum / $womancount;
$totalAverage = ($mansum + $womansum) / ($mancount + $womancount);
?>

how to use biasedNumberBetween faker? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to get a number between 1 to 6 with more chances to be close to 1.
i have tried this:
<li>{{Faker\Factory::create()->biasedNumberBetween($min = 10, $max = 20, $function = 'unbiased')}}</li>
What i am trying to do is to generate a number from 1 to 6 rand(1,6); but make the numbers be closer to one as the lower numbers will have more weight than the others.
Something like this ?
<?php
function weightedRand($min, $max, $weightedMax) {
$arr = array();
for($i = 0; $i < 10; $i++) {
$arr[] = rand($min, $weightedMax);
}
$arr[] = rand($min, $max);
return $arr[rand(0,10)];
}
echo weightedRand(1,6, 3);
?>
numbers below 4 will now be more likely than numbers above :)

How To Display Random Results in PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Im trying to display 2 or more unique random results from a text file, How do I do that please?
But I need unique results and not 2 or more of the same.
Currently using this code, but it only returns 1 random result:
<?php
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$randArrayIndexNum = array_rand($textArray);
$randPhrase = $textArray[$randArrayIndexNum];
?>
<?php echo $randPhrase ?>
I would use something like that
shuffle($textArray);
echo $textArray[0];
echo $textArray[1];
http://php.net/manual/tr/function.shuffle.php
You can give a shot to this also. Code is not tested. I am collecting the used into an array, and check, is that used before.
$text = file_get_contents('flatFileDB.txt');
$textArray = explode("\n", $text);
$used = array();
$countOfRandoms = 2;
$randoms = array();
$i = 1;
do {
if ($countOfRandoms == $i) {
break;
}
$randArrayIndexNum = array_rand($textArray);
if (in_array($randArrayIndexNum, $used)) {
continue;
}
$used[] = $randArrayIndexNum;
$random = $textArray[$randArrayIndexNum];
$i++;
} while (true);

How to increment a for loop by more than 1? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How do I print a series like this?
6,15,24,33,42.....1000
I have tried using a for loop like this, but can't work out how to get it to increment by 9 on each iteration.
for($i = 6; $i <= 1000; $i = 9)
{
echo $i . ', ';
}
Quite simple really:-
for($i = 6; $i <= 1000; $i += 9){
echo $i . ', ';
}
//If you have to finish the series with 1000, although it is not a part of the series:-
echo '1000';
See it working
just for fun:
echo implode(', ', range(6, 1000, 9));
echo ", 1000";
<?php
for($i=6; $i<1000; $i+=9)
echo $i."<br>";
?>
Seeing the question this can be an answer.

Categories