eon201 Posted November 16, 2007 Share Posted November 16, 2007 Hi, Im attempting something which I have NO idea how to do... I have an array like so... <?php // Now calculate top 5 pages this day foreach ($url_total_today as $key2 => $value2){ echo "$value2<br/>"; } ?> It spits out a ridiculous amount of urls. Probably around 500. What I would like it to do is to count how many times each url appears and display it plainly like so... 142 - url1.php 67 - url2.html 56 - url3.htm 23 - url4.php 5 - url5.php Can anybody help me with this? Ive tried using the 'array_unique' funtion in conjunction with the 'count' function but no joy. ??? Thanks In advance. Eon201 Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/ Share on other sites More sharing options...
kenrbnsn Posted November 16, 2007 Share Posted November 16, 2007 You probably want to look at the array_count_values()[/function] Ken Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-393030 Share on other sites More sharing options...
eon201 Posted November 16, 2007 Author Share Posted November 16, 2007 hmm. Ok half way there now. Thanks How would I only display the top five?? Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-393033 Share on other sites More sharing options...
eon201 Posted November 16, 2007 Author Share Posted November 16, 2007 Ok I have achieved it with the asort function. Problem is I need to work out how to get it to display only the first five iterations of the loop. Does anyone know how to stop a loop at a certain point?? Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-393043 Share on other sites More sharing options...
pocobueno1388 Posted November 16, 2007 Share Posted November 16, 2007 To stop a loop, you can use break; Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-393046 Share on other sites More sharing options...
kenrbnsn Posted November 16, 2007 Share Posted November 16, 2007 If you only want the top five, just do 5 iterations of the loop: <?php $test_ary = range(99,1); for ($i=0;$i<5;$i++) echo $test_ary[$i] . "<br>\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-393056 Share on other sites More sharing options...
eon201 Posted November 20, 2007 Author Share Posted November 20, 2007 Thanks everybody! Solved now. Link to comment https://forums.phpfreaks.com/topic/77643-solved-calculate-top-5-in-array/#findComment-395038 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.