Jump to content

probelms finding min max within an array


adamgonge

Recommended Posts

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/>";}?>

Link to comment
Share on other sites

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/>";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.