adamgonge Posted February 26, 2014 Share Posted February 26, 2014 hello, Im stuck on a problem with my code. the program takes a csv file and breaks it down to use. I am using an array and displaying the dates and the differences between each open/close and hi/lo. My code does that much now after thats complete i want to display the highest and lowest of both the hi/lo and open/close. open/close is assigned to $dif_o hi/lo assigned to $dif. Ive tried the min max function but that's not working. not sure how to go about doing this heres my code: <?php//read the data from the file and display player's name and team$fp = fopen("http://cs.uww.edu/data/stocks/AAPL.csv", 'r');if(!$fp){ echo "could not open the file!"; exit(); } displayData($fp); function displayData($fp){//define an array to store data $stocks = array(); //read each line into an array, save name and team using an associative arraywhile(!feof($fp)){ // read the current line $info = fgetcsv($fp, 250, ','); // add dataa only if data is nonempty if ($info[0] !=""){ // read the date into a variable $date = $info[0]; // add this information to associative array //use $date as the key $dif =($info[2])-($info[3]); $dif_o=$info[1]-$info[4]; $stocks[$date]= $dif; $stocks[$date]=$dif_o; }// end if echo "Date: ".$date.", Difference between Hi and Lo: ".$dif.", Difference between Open and Close: ".$dif_o."<br/>";}//end while//sort data $min=min($dif);$max=max($dif); echo "Min: ".$min." Max: ".$max."<br/>";}?> Quote Link to comment https://forums.phpfreaks.com/topic/286532-probelms-finding-min-max-within-an-array/ Share on other sites More sharing options...
Ch0cu3r Posted February 26, 2014 Share Posted February 26, 2014 You'll need to add the hi/lo difference to an array and then use min/max on that array eg function displayData($fp) { //define an array to store data $stocks = array(); //read each line into an array, save name and team using an associative array while(!feof($fp)) { // read the current line $info = fgetcsv($fp, 250, ','); // add dataa only if data is nonempty if ($info[0] !="") { // read the date into a variable $date = $info[0]; // add this information to associative array //use $date as the key $dif = $info[2]-$info[3]; $dif_o = $info[1]-$info[4]; $stocks['dif'][] = $dif; // add hi/low differenc to array $stocks['dif_o'][] = $dif_o; // add open/close differenc to array }// end if echo "Date: ".$date.", Difference between Hi and Lo: ".$dif.", Difference between Open and Close: ".$dif_o."<br/>"; }//end while // get the min/max hi/lo difference $min=min($stocks['dif']); $max=max($stocks['dif']); echo "<b>DIF</b> Min: ".$min." Max: ".$max."<br/>"; // get the min/max open/close difference $min=min($stocks['dif_o']); $max=max($stocks['dif_o']); echo "<b>DIF_O</b> Min: ".$min." Max: ".$max."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/286532-probelms-finding-min-max-within-an-array/#findComment-1470731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.