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 Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/ 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]; ?> Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929451 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? Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929462 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 Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929468 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]; ?> Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929469 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); Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929471 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. Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929475 Share on other sites More sharing options...
puretony Posted October 3, 2009 Author Share Posted October 3, 2009 how about adding average into display? Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929481 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); Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929484 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? Link to comment https://forums.phpfreaks.com/topic/176345-sorting-price-list/#findComment-929491 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.