a sum of some values in an php array [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 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);
?>

Related

Case Looping in PHP [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 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/>" ;
}
?>

Get the amount of elements higher than a particular one [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 6 years ago.
Improve this question
I have this in my php code:
$aNumbers = array(2,8,9,5.5,4,5.5,6,7,8,9,10,1,'NB');
From this I must determine the amount of values above 5.5 and the average of all numbers combined. Using only a For Loop, if/else construction and the count() function.
I have tried to solve this myself, but nowhere on the internet I can find sufficient information.
This should do it:
$aNumbers = array(2,8,9,5.5,4,5.5,6,7,8,9,10,1,'NB');
const HIGH_VALUE_CUTOFF = 5.5;
$uncleanArrayCount = count($aNumbers);
$cleanArray = array();
$highValueCount = 0;
$sum = 0;
for ($i = 0; $i < $uncleanArrayCount; $i++) {
if ($aNumbers[$i] > 0) { // catch only numbers
$sum += $aNumbers[$i];
$cleanArray[] = $aNumbers[$i]; // builds an array of numbers
}
if ($aNumbers[$i] > HIGH_VALUE_CUTOFF) {
$highValueCount++;
}
}
$average = $sum / count($cleanArray);
echo "Average: $average <br />";
echo "Numbers above " . HIGH_VALUE_CUTOFF . ": $highValueCount";
Next time, post what you've tried. 'Twas fun doing your homework for you!
Try something like this:
<?php
$sum = 0;
$amount = 0;
$aNumbers = array(2, 8, 9, 5.5, 4, 5.5, 6, 7, 8, 9, 10, 1, 'NB');
foreach ($aNumbers as $value){
if($value > 5){
$sum += $value;
$amount++;
}
}
echo "Sum : " . $sum . " - Average : ". ($sum / $amount);
?>

How to get count 7 days data of mysql more efficient to chart [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 need get 7days "date text" and "data count" for chart.js create a chart.
mysql table
id
title
create_time (unixtime int10)
php
//get 7days
$now = time();
$day7 = array();
$day7_srt="";
for($i=0; $i<7; $i++){
$d = mktime(0,0,0,date('m',$now),date('d',$now)-$i);
$day7[$i]["d"] = date('d',$d);
$day7[$i]["m"] = date('m',$d);
}
sort($day7);
foreach ($day7 as $key => $value) {
$d = $value['d'];
$m = $value['m'];
$day7_srt .= "\"$m-$d\",";
}
$day7_srt=substr("$day7_srt",0,-1); //"11-19","11-20","11-21".....etc
//get data count
$data_value = array();
$date_show ="";
foreach ($day7 as $key => $value) {
$date = $value["date"];
$sql = "SELECT COUNT( * ) FROM `news` WHERE FROM_UNIXTIME( `create_time` , '%Y-%m-%d' ) = '$date'";
$r_title = $pdo->query($sql);
$r_title = $r_title->fetch();
$data_value[$key] = $r_title["0"];
}
foreach ($data_value as $key => $value) {
$date_show .= "\"$value\",";
}
$date_show=substr("$date_show",0,-1); //show "3","5","2".....etc
Js code style
var lineChartData = {
labels : ["11-19","11-20","11-21","11-22","11-23","11-24","11-25"],
datasets : [
{
label: "My Chart",
data : [1,2,3,4,5,7,9] //mysql data here
}
]
}
I can do my idea, and it's work
But I feel maybe can more efficient do that?
any sugesst?

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

Categories