I would like to find the biggest sum in associative array. I have information for 2 years and i have found the sum for each year and now I want to find which one is bigger and which year this sum belongs to.
<?php
$year = array (
"Year 2015 " => array(
"Televizor " => "3",
"Lavatrice" => "4",
"Kompjuter" => "5",
"printer" => "5",
),
"Year 2016 " => array(
"Televizor " => "3",
"lavatrice" => "7",
"kompjuter" => "4",
"printer" => "1",
)
);
foreach($year as $key => $product){
echo "<br>";
echo "$key";
echo"<table border=1 cellspacing=0>
<tr>
<td>Produkti</td>
<td>Sasia</td>
</tr>";
echo "<br>";
foreach( $product as $key => $value){
echo "<tr>
<td>$key</td>
<td>$value</td>
</tr>";
}
echo "</table>";
}
foreach($year as $key => $product){
echo"$key";
$arrayOfValues=array_values($product);
$arraySum=array_sum($arrayOfValues);
$avg=$arraySum/count($arrayOfValues);
echo "Average=$avg";
echo " ";
$maxValueArray=array();
array_push($maxValueArray, $arraySum);
echo "Sum=$maxValueArray[0]";
echo "<br>";
}
?>
and this is the output :
Year 2015
Produkti Sasia
Televizor 3
Lavatrice 4
Kompjuter 5
printer 5
Year 2016
Produkti Sasia
Televizor 3
lavatrice 7
kompjuter 4
printer 1
Year 2015 Average=4.25 Sum=17
year 2016 Average=3.75 Sum=15
i would like to print "Year 2015 has the biggest sum =17 "
To do it, you just need to create an array with the valuation.
$yearSumValues = array_map('array_sum', $year);
$max = max($yearSumValues);
$maxYearKey = array_search($max, $yearSumValues);
echo 'Max sum is: ', $max, '<br />';
echo 'Data key is: ', $maxYearKey, '<br />';
if( !is_null($maxYearKey) ) {
echo 'Values are: ', var_dump($year[$maxYearKey]);
}
So, now, in $max you will have the biggest value. So, $maxYearKey contains the key of the biggest year (be careful with equalities).
var_dump($year[$maxYearKey]);
Produkti Sasia
Televizor 3
Lavatrice 4
Kompjuter 5
printer 5
Compact version
$yearSumValues = array_map('array_sum', $year);
if( ($maxYearKey = array_search(max($yearSumValues), $yearSumValues)) != null ) {
echo 'found ', $maxYearKey, ' with values ', var_dump($year[$maxYearKey]);
}
You can play: http://phplab.io/lab/IgJkR
Short solution using array_reduce, array_sum and array_search functions:
$result = array_reduce($year, function ($prev, $item) {
return (array_sum($prev) > array_sum($item))? $prev : $item;
}, []);
print_r(array_search($result, $year) ." has the biggest sum = ". array_sum($result));
The output:
Year 2015 has the biggest sum = 17
http://php.net/manual/en/function.array-reduce.php
Related
What do I need to do to convert the number only if all the number matches?
At the moment, it's converting to each number. (I've tried to do a lot and I didn't succeed)
I would like to know if have any way of adapting to only show the word if the number "is complete", otherwise show the word "Doesn't contain a name record".
Example:
When I try to use the number 1987, the output its: Name1 Name9 Name8 Name7, and I want to be only one word: Name1987.
function numberTowords($num)
{
$ones = array(
0 => "Name0",
1 => "Name1",
2 => "Name2",
3 => "Name3",
4 => "Name4",
5 => "Name5",
6 => "Name6",
7 => "Name7",
8 => "Name8",
9 => "Name9",
10 => "Name10",
11 => "Name11",
12 => "Name12",
13 => "Name13",
14 => "Name14",
15 => "Name15",
16 => "Name16",
17 => "Name17",
18 => "Name18",
19 => "Name19",
1987 => "Name1987",
2398 => "Name2398",
);
$num = number_format($num, 2, ".", ",");
$num_arr = explode(".", $num);
$wholenum = $num_arr[0];
$decnum = $num_arr[1];
$whole_arr = array_reverse(explode(",", $wholenum));
krsort($whole_arr, 1);
$rettxt = "";
foreach ($whole_arr as $key => $i) {
while (substr($i, 0, 1) == "0")
$i = substr($i, 1, 5);
if ($i < 20) {
/* echo "getting:".$i; */
$rettxt .= $ones[$i];
} elseif ($i < 100) {
if (substr($i, 1, 1) != "0") $rettxt .= " " . $ones[substr($i, 1, 1)];
} else {
if (substr($i, 0, 1) != "0") $rettxt .= $ones[substr($i, 0, 1)];
if (substr($i, 2, 1) != "0") $rettxt .= " " . $ones[substr($i, 2, 1)];
}
if ($key > 0) {
$rettxt .= " ";
}
}
if ($decnum > 0) {
$rettxt .= " and ";
if ($decnum < 20) {
$rettxt .= $ones[$decnum];
} elseif ($decnum < 100) {
$rettxt .= " " . $ones[substr($decnum, 1, 1)];
}
}
return $rettxt;
}
only show the word if the number "is complete", otherwise show the word "Doesn't contain a name record".
Example: ... number 1987 I want (output) to be only one word: Name1987.
If this is all you need - piece of cake:
<!-- this is only a tool for easy test -->
<form><input name="nr"><input type="submit"></form>
<?php
$pattern = array_merge(range(0,19),[1987,2398]);
$nr = $_GET[nr];
echo in_array($nr,$pattern) ? "Name$nr" : "Doesn't contain a name record";
array_merge() joins arrays, range() is kind of array, in_array() checks if a value exists in an array
more flexible - for cases you omitted in your question
$pattern = [0=>'my name is Zero',
1=>'I am the ONE',
12345=>'this is a number',
'12345'=>'this is a string',
'7x24'=>'all time shift',
999=>'almost thousand',
12=>'call me a dozen'];
echo array_key_exists($nr,$pattern) ? $pattern[$nr] : "Doesn't contain a name record";
// if you don't like shorthand - different notation:
if(array_key_exists($nr,$pattern)){ echo $pattern[$nr];}
else{ echo "Doesn't contain a name record";}
array_key_exists()
Arrays are fast and nice in use. Built-in functions are optimised and that makes them much better than step-by-step manipulations. Debugging a few lines of code is easy, debugging plenty lines of particular manipulations is a nightmare. For most cases in php the only concern is to find the name of built-in function which do it all at once.
I didn't want to limit the numbers (...) want to add more than 200 ids
I didn't limit anything, and it is not that much, even as associative array it wouldn't be big array
more complex - it's time to organise the code structure
function name($x,$arr){
if(in_array($x,range(130,150))){ $name = 'from 130 to 150';} //or (($x >= 130) && ($x <= 150)), or ... etc
else if(in_array($x,range(100,200))){ $name = 'from 100 to 200';}
else if(array_key_exists($x,$arr)){ $name = $arr[$x];}
else{ $name = 'pass';}
$z = ($x[0] == 0) ? 'leading zero' : '∗'; //or if-else instead of Elvis, (substr($x,0,1) == 0) ...
return($z.' '.$name);}
echo '<p>'.name($nr,$pattern).'</p>';
In the name() function, sequence is important for the sub-ranges.
ternary operator a.k.a. Elvis
filter_var() if you like it
I have the following php array and I am trying to print a table with 4 columns. All is OK until the array has 4, 8, 12 and etc. keys /values but when array has length that can not be divided by 4 becomes the problem. Here is the sample array:
$newarray =array(
"make"=> "Ford" ,
"model"=> "S-MAX" ,
"model_year"=> "2009" ,
"made"=> "2010-01-01" ,
"manufacturer"=> "Ford Werke AG" ,
"manufacturer_address"=> "Koeln-Niehl, Germany" ,
"body"=> "Sedan/Saloon" ,
"engine_power_kw"=> "142" ,
"engine_displacement_ccm"=> "2000" ,
"engine_full"=> "2.0L Duratorq-TDCi (143PS) - DW"
);
and the code that prints the table:
$rama_result.='<table class="table w100 customtable">';
$i=1;
foreach($newarray as $key=>$value){
$rama_result1.= '<th>'.$key.'</th>';
$rama_result2.= '<td>'.$value.'</td>';
if($i % 4 == 0){
$rama_result.='</tr><tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr><tr>';
$rama_result1 = '';$rama_result2 = '';
}
$i++;
}
$rama_result.='</tr></table>';
Can you please help and show me the way to print the missing 2 in the last row so the code to work correct. Thank you for your help
The code above prints this table
<table class="table w100 customtable"><tbody>
<tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr>
<tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01</td></tr><tr></tr>
<tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr>
<tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td> <td>142</td></tr>
<tr></tr></tbody></table>
<table class="table w100 customtable"><tbody>
<tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr>
<tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01</td></tr><tr></tr>
<tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr>
<tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td> <td>142</td></tr>
<tr></tr></tbody></table>
Instead of looping each value use array_chunk to split the array then implode the headers and values.
$arr = array_chunk($newarray, 4, true);
echo "<table>\n";
foreach($arr as $sub){
echo "<tr><th>" . implode("</th><th>", array_keys($sub)) . implode("</th><th>", array_slice([" ", " ", " "],0 , 4-count($sub))) . "</th></tr>\n";
echo "<tr><td>" . implode("</td><td>", $sub) . implode("</td><td>", array_slice([" ", " ", " "],0 , 5-count($sub))) ."</td></tr>\n";
}
echo "</table>";
Output:
<table>
<tr><th>make</th><th>model</th><th>model_year</th><th>made</th></tr>
<tr><td>Ford</td><td>S-MAX</td><td>2009</td><td>2010-01-01 </td></tr>
<tr><th>manufacturer</th><th>manufacturer_address</th><th>body</th><th>engine_power_kw</th></tr>
<tr><td>Ford Werke AG</td><td>Koeln-Niehl, Germany</td><td>Sedan/Saloon</td><td>142 </td></tr>
<tr><th>engine_displacement_ccm</th><th>engine_full </th><th> </th></tr>
<tr><td>2000</td><td>2.0L Duratorq-TDCi (143PS) - DW </td><td> </td><td> </td></tr>
</table>
https://3v4l.org/TrCnM
Edited to include the empty cells.
You could just extend the array to have a multiple of 4 elements before you start:
for ($c = count($newarray); $c % 4 != 0; $c++) {
$newarray[str_repeat("\n", $c % 4)] = '';
}
$rama_result.='<table class="table w100 customtable">';
$i=1;
// ...
Demo on 3v4l.org
After the loop, you could check that there is something still in $rama_result1 - which would mean there is data left to be added into the table. This code pads the existing content with enough empty cells to make it up to the 4 columns (you can tweak the content if desired in the str_repeat() calls).
if ( $rama_result1 != '' ) {
$i--;
$rama_result1 .= str_repeat("<th />", 4-($i%4));
$rama_result2 .= str_repeat("<td />", 4-($i%4));
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>';
}
Also (as Nick pointed out) there are extra <tr> tags in various places. I've updated a few other parts of the code to try and tidy up the generated HTML...
$i=1;
foreach($newarray as $key=>$value){
$rama_result1.= '<th>'.$key.'</th>';
$rama_result2.= '<td>'.$value.'</td>';
if($i % 4 == 0){
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>'.PHP_EOL;
$rama_result1 = '';
$rama_result2 = '';
}
$i++;
}
if ( $rama_result1 != '' ) {
$i--;
echo ($i%4).PHP_EOL;
$rama_result1 .= str_repeat("<th />", 4-($i%4));
$rama_result2 .= str_repeat("<td />", 4-($i%4));
$rama_result.='<tr>'.$rama_result1.'</tr><tr>'.$rama_result2.'</tr>'.PHP_EOL;
}
$rama_result.='</table>';
I am trying to query mysql db using values passed in an array. The problem is the first element produces two results. Here is my code and results.
$common = array_intersect($ingredients, $ingredients2);
unset($common['1']);
$unique = array_unique($common);
echo "The common array is:";
print_r(array_count_values($common));
echo "<br> The unique array is :";
print_r($unique);
echo "<br>";
echo extract($unique)."<br>";
$i = 0;
$sum = 0;
foreach (array_count_values($common) as $key => $value) {
$keys = "$key";
$value = "$value";
echo "$keys : ";
echo "$value<br>";
$i++;
$sql = mysql_query("SELECT * FROM `ingredients` WHERE `name` = '$keys'") or die(mysql_error());
while ($row = mysql_fetch_array($sql)) {
$price = $row['price'];
echo "The price is : $price<br>";
$total_price = $value*$price;
echo "The total price for $keys is : $total_price<br>";
$sum+= $total_price;
}
}
echo "The sum of the prices is $sum";
Here is what I get:
The common array is:Array ( [test] => 3 [ugali] => 2 [mboga] => 2 [0] => 1 )
The unique array is :Array ( [0] => test [2] => ugali [4] => mboga [8] => 0 )
0
test : 3
The price is : 100
The total price for test is : 300
The price is : 100
The total price for test is : 300
ugali : 2
The price is : 100
The total price for ugali is : 200
mboga : 2
The price is : 4
The total price for mboga is : 8
0 : 1
The sum of the prices is 808
I got the answer to this. I removed the while loop and it worked instead of
while ($row = mysql_fetch_array($sql)) {}
I have
$row = mysql_fetch_array($sql);
That solved the problem.
I've been trying for a while but still, I'm stuck here. Actually I need to get rank from average values. Here my result
Average Rank
39 39 rank is 1
32 32 rank is 1
51 51 rank is 1
57 57 rank is 1
what I really need is
Average Rank
39 3
32 4
51 2
57 1
I've tried several method, but nothing seem to be works. Btw Here's my code
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$array = array($avg);
$i=1;
foreach($array as $key=>$value)
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
And Here's the full Code
<?php $nama_mapel=$ux['nama_mapel'];
$an = mysql_fetch_array(mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas' and nama_mapel='$nama_mapel'"))?>
<text style="display:none;"><?php if($NILAI_AKHIR!=0){$NILAI_AKHIR4=substr($NILAI_AKHIR/100*4,0,4); echo $NILAI_AKHIR4;} ?></text>
<tr>
<td width="5%" class="ki1234" height="auto" valign="center" align="center"><?php echo $no++ ;?></td>
<td width="40%" class="ki1234" height="auto" valign="center" align="left"> <?php echo $mmm['nama'];?></td>
<td width="10%" class="ki1234" height="auto" valign="center" align="center"><?php echo $nis; ?></td>
<?php
$mapelz=mysql_query("SELECT*FROM mapel where kode_kelas='$kode_kelas' and id_tp='$id_tp'");
$arrg=[];
while($uxe = mysql_fetch_array($mapelz)){
$id_mapel=$uxe['id_mapel'];
//awal nilai akhir//
$dren = mysql_query("SELECT*FROM nilai_siswa,data_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=data_siswa.nis and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_tugas=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm; };
$queryj=mysql_query("SELECT sum(n3_100) as rn3_100 FROM nilai_siswa,penilaian_judul where nilai_siswa.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa.nis=$nis and penilaian_judul.id_mapel=$id_mapel and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn3_100=substr($hasil['rn3_100']/$jumlah,0,5); $RATA_TUGAS=$rn3_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uh=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=2 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UH= $rn_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_uts=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=3 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_UTS= $rn_100;}
$dren = mysql_query("SELECT*FROM nilai_siswa2,data_siswa,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=data_siswa.nis and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp' order by data_siswa.id_siswa asc"); $jm=mysql_num_rows($dren); $jumlah_us=$jm;
if($jm==0){$jumlah="1";} else {$jumlah=$jm;};
$queryj=mysql_query("SELECT sum(n_100) as rn_100 FROM nilai_siswa2,penilaian_judul where nilai_siswa2.id_penilaian_judul=penilaian_judul.id_penilaian_judul and nilai_siswa2.nis=$nis and penilaian_judul.id_mapel=$id_mapel and nilai_siswa2.jenis=4 and penilaian_judul.id_semester='$id_semester' and penilaian_judul.id_tp='$id_tp'");
while($hasil=mysql_fetch_array($queryj))
{$rn_100=substr($hasil['rn_100']/$jumlah,0,5); $RATA_US= $rn_100;}
if(!isset($RATA_TUGAS)){$RATA_TUGAS2=0;}else{$RATA_TUGAS2=$RATA_TUGAS;};
if(!isset($RATA_UH)){$RATA_UH2=0;}else{$RATA_UH2=$RATA_UH;};
if(!isset($RATA_UTS)){$RATA_UTS2=0;}else{$RATA_UTS2=$RATA_UTS;};
if(!isset($RATA_US)){$RATA_US2=0;}else{$RATA_US2=$RATA_US;};
$jumlahc=($persen['tugas']*$RATA_TUGAS2)/100+($persen['uh']*$RATA_UH2)/100+($persen['uts']*$RATA_UTS2)/100+($persen['us']*$RATA_US2)/100;
if($jumlah_tugas!=0){$k=1;} else {$k=0;};
if($jumlah_uh!=0){$l=1;} else {$l=0;};
if($jumlah_uts!=0){$m=1;} else {$m=0;};
if($jumlah_us!=0){$n=1;} else {$n=0;};
$JUMLAH_PERSEN1=($persen['tugas']*$k)+($persen['uh']*$l)+($persen['uts']*$m)+($persen['us']*$n);
if($JUMLAH_PERSEN1==0){$JUMLAH_PERSEN=1;}else{$JUMLAH_PERSEN=$JUMLAH_PERSEN1;};
$NILAI_AKHIRg=substr(($jumlahc/$JUMLAH_PERSEN)*100,0,5);
$nile= $NILAI_AKHIRg;
$arrg[] = $NILAI_AKHIRg;} ?>
<?php $p = mysql_fetch_array(mysql_query("SELECT * FROM peringkat WHERE kode_kelas='$kode_kelas' and nis='$nis'and id_tp='$id_tp'")); ?>
<text style="display:none;"> <?php $sumc = 0; $output = ""; while($element = array_shift($arrg)){$sumc += $element; $output .= $element; if(sizeof($arrg) > 0){ $output .= " + ";}} echo $output . PHP_EOL;?></text>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php echo $sumc;?></td>
<text style="display:none;"><?php $result=mysql_query("SELECT count(*) as total FROM mapel WHERE kode_kelas='$kode_kelas' and kel!=3"); $data=mysql_fetch_assoc($result); echo $data['total'];?></text>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?></td>
<td width="15%" class="ki1234" height="auto" valign="center" align="center"><?php
$array = array($avg);
$i=1;
foreach($array as $key=>$value)
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
?></td>
</tr>
<?php }?>
Need Your help.. Anyone..?!
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$array = array($avg); // useless
$i=1;
foreach($array as $key=>$value) // also useless, you never use these variables, and since $array contains only one entry, there is no need to foreach it
{
$max = max($array);
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
Let's clean that up :
<?php $avg = ($sumc)/($data['total']); echo number_format((float)$avg, 2, ',', ''); ?>
<?php
$max = $avg; // That's actually what you were doing. Cleaning the code makes it obvious, that's all
echo "\n".$max." rank is ". $i."\n";
$keys = array_search($max, $array);
unset($array[$keys]);
if(sizeof($array) >0)
if(!in_array($max,$array))
$i++;
}
The code still has errors, but the more we correct, the more its design flaws appear, and the less it's therefore correctable.
You should, in my opinion, look at that code, figure a way to put all the values you want to have in $array in it so that then you can compute the datas you need.
From what you've posted seems the problem is how to sort the average values descending.
With this in mind you know that if you're calculating the average on the go, you have to go throuh the entire array first so here's some snippet to help you:
// Random values I want to calculate the average
$values = [
1 => [15, 25, 35],
2 => [45, 55, 65],
3 => [75, 85, 95],
4 => [105, 115, 125]
];
$avgs = array();
foreach ($values as $key => $value) {
$avgs[$key] = array_sum($value) / count($value); // calculate average for each key on the array
}
print_r($avgs);
// Will print:
// Array
// (
// [1] => 25
// [2] => 55
// [3] => 85
// [4] => 115
//)
arsort($avgs); // sort the array in descending order
print_r($avgs);
// Will print:
//Array
//(
// [4] => 115
// [3] => 85
// [2] => 55
// [1] => 25
//)
So there if have your avg array sorted, therefore you have your "rank".
ps. The keys 1, 2, 3, 4 are just random values, you could have anything there.
As the title says what I'm trying to do is count the number of images in a directory and output it as numbers so for example if there are 4 images I want the result to be:
01 | 02 | 03 | 04
I have this so far:
$count = glob('images/{*.jpg}', GLOB_BRACE);
foreach($count as $filecount) {
echo '<li>' . $filecount . '</li>';
}
which outputs path/filename.jpg but haven't a clue on how to convert that to a numbers array or even if i'm in the right ballpark.
As usual all help is appreciated and thanks in advance.
That array is numerically indexed (0 to length-1 ), use it to obtain the number:
foreach($count as $index => $filecount) {
$number = $index+1;
foreach($count as $index => $filecount) {
// $number is "01" for the first and "02" for second etc
$number = str_pad($index, 2, "0", STR_PAD_LEFT);
//...
}