puretony Posted October 3, 2009 Share Posted October 3, 2009 i'd like to know how get the cheapest and most expensive prices from the list of prices below random prices $5.70 $8.40 $1.30 $15.50 $10.10 Quote Link to comment Share on other sites More sharing options...
lipun4u Posted October 3, 2009 Share Posted October 3, 2009 <?php $price = array(5.70, 8.40, 1.30, 15.50, 10.10); sort($price); echo "Cheapest : \$" . $price[0] ; echo "<br/>\n"; echo "Most expensive : \$" . $price[count($price)-1]; ?> Quote Link to comment Share on other sites More sharing options...
puretony Posted October 3, 2009 Author Share Posted October 3, 2009 it works but what if i read from a text file? Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted October 3, 2009 Share Posted October 3, 2009 it works but what if i read from a text file? same thing lol Quote Link to comment Share on other sites More sharing options...
Amtran Posted October 3, 2009 Share Posted October 3, 2009 Separate the prices by dashes or commas: 1.10-1.15-2.50-3.45-7.10-8.90 Then... <?php $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $file = fread($handle, filesize($filename)); $prices = explode('-', $file); sort($prices); echo "Cheapest : \$" . $price[0] ; echo "<br/>\n"; echo "Most expensive : \$" . $price[count($price)-1]; ?> Quote Link to comment Share on other sites More sharing options...
jmaker Posted October 3, 2009 Share Posted October 3, 2009 Would this work? $price = array(5.70, 8.40, 1.30, 15.50, 10.10); sort($price); $cheap = array_shift($price); $expensive = array_pop($price); Quote Link to comment Share on other sites More sharing options...
Amtran Posted October 3, 2009 Share Posted October 3, 2009 Yep, that will give you the most and least expensive prices. Quote Link to comment Share on other sites More sharing options...
puretony Posted October 3, 2009 Author Share Posted October 3, 2009 how about adding average into display? Quote Link to comment Share on other sites More sharing options...
Amtran Posted October 3, 2009 Share Posted October 3, 2009 <?php $price = array(5.70, 8.40, 1.30, 15.50, 10.10); $average = array_sum($price) / count($price); Quote Link to comment Share on other sites More sharing options...
puretony Posted October 3, 2009 Author Share Posted October 3, 2009 how to create an output file namead Outwine.txt after completing process? Quote Link to comment 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.