Page 1 of 1
Quick Question About Arrays
Posted: Fri Aug 21, 2009 7:19 am
by tabutcher
hi,
I am a php beginner. Briefly what I want to do is find the largest value stored in an array but have run into a problem. I've tried the max function and also a for loop but have not been successful. Basically my problem is that the code returns only numbers under 100. For example if the array looked like this:
Array ( [0] => 15 [1] => 10 [2] => 30 [3] => 20 [4] => 100 [5] => 40)
the value would return 40 and not the 100.
below is my code:
for($intCount=0; $intCount<count($arMark); $intCount++){
if($arMark[$intCount] >= $intHighest)
$intHighest = $arMark[$intCount];
}
thanks in advance
Re: Quick Question About Arrays
Posted: Fri Aug 21, 2009 7:53 am
by jackpf
Max() works for me. Check this out:
Code: Select all
<?php
$array = array(
10,
20,
8,
83,
204,
3005
);
echo max($array);
Re: Quick Question About Arrays
Posted: Fri Aug 21, 2009 9:02 am
by tabutcher
I just tried you're example in a new file and it works fine but can't seem to find the reason why it is not working with my code. Below is my complete code: it would be great if someone could pick out the problem.... thank you
<html>
<head>
<title>PHP Mark Script</title>
</head>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method="POST">
<label for="firstname">Firstname: </label>
<input type="text" name="firstname" id="firstname" />
<br />
<label for="surname">Surname: </label>
<input type="text" name="surname" id="surname" />
<br />
<label for="mark">Mark: </label>
<input type="text" name="mark" id="mark" />
<br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
//Get values from form
$strFirstname = $_POST["firstname"];
$strSurname = $_POST["surname"];
$strMark = $_POST["mark"];
//Open file for writing
$marksFile = fopen("marks.txt", 'a');
fwrite($marksFile, $strFirstname." ");
fwrite($marksFile, $strSurname." = ");
fwrite($marksFile, $strMark);
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Initialise arrays
$arName = array();
$arMark = array();
$intCounter = 0;
//Open file
//Reads each line of the file and breaks them apart into two array elements
//Each array element is stored in two seperate arrays (name and mark)
$marksFile = fopen("marks.txt", 'r');
while(!feof($marksFile)){
$strRecord = fgets($marksFile);
$arParts = explode('=', $strRecord);
$arName[$intCounter] = $arParts[0];
$arMark[$intCounter] = $arParts[1];
echo $arName[$intCounter];
echo $arMark[$intCounter];
echo "<br />";
$intCounter++;
}
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Open file for writing
$marksFile = fopen("marks.txt", 'a');
fwrite($marksFile, "\r\n");
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Display values
echo "<br />";
echo "Average: ".array_sum($arMark)/count($arMark)."<br />";
echo "Total: ".array_sum($arMark)."<br />";
print_r(array_values($arMark))."<br />";
echo "<br />";
echo max($arMark);
}
?>
</body>
</html>
Re: Quick Question About Arrays
Posted: Fri Aug 21, 2009 10:17 am
by jackpf
Aha, bit of a dodgy problem. After var_dump()-ing the array, it seems that all the results are strings...so it can't calculate the max value, since they're all strings.
Type casting the value to an int seems to work.
Try this:
Code: Select all
<html>
<head>
<title>PHP Mark Script</title>
</head>
<body>
<form action='<?php echo $_SERVER["PHP_SELF"]; ?>' method="POST">
<label for="firstname">Firstname: </label>
<input type="text" name="firstname" id="firstname" />
<br />
<label for="surname">Surname: </label>
<input type="text" name="surname" id="surname" />
<br />
<label for="mark">Mark: </label>
<input type="text" name="mark" id="mark" />
<br />
<input type="submit" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
//Get values from form
$strFirstname = $_POST["firstname"];
$strSurname = $_POST["surname"];
$strMark = $_POST["mark"];
//Open file for writing
$marksFile = fopen("marks.txt", 'a');
fwrite($marksFile, $strFirstname." ");
fwrite($marksFile, $strSurname." = ");
fwrite($marksFile, $strMark);
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Initialise arrays
$arName = array();
$arMark = array();
$intCounter = 0;
//Open file
//Reads each line of the file and breaks them apart into two array elements
//Each array element is stored in two seperate arrays (name and mark)
$marksFile = fopen("marks.txt", 'r');
while(!feof($marksFile)){
$strRecord = fgets($marksFile);
$arParts = explode('=', $strRecord);
$arName[$intCounter] = $arParts[0];
$arMark[$intCounter] = (int) $arParts[1];
echo $arName[$intCounter];
echo $arMark[$intCounter];
echo "<br />";
$intCounter++;
}
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Open file for writing
$marksFile = fopen("marks.txt", 'a');
fwrite($marksFile, "\r\n");
if(!fclose($marksFile))
echo "<p>Error Closing File</p>";
//Display values
echo "<br />";
echo "Average: ".array_sum($arMark)/count($arMark)."<br />";
echo "Total: ".array_sum($arMark)."<br />";
print_r($arMark)."<br />";
echo "<br />";
echo max($arMark);
}
?>
</body>
</html>
Re: Quick Question About Arrays
Posted: Fri Aug 21, 2009 10:24 am
by tabutcher
Works a charm. Thank you so much I've been trying to get this to work all day.
