npsari Posted January 2, 2009 Share Posted January 2, 2009 Hello there I have a string like that... $string = "224 556 665 224 667 443 776 443 222 443 667 224 997 665 009 443 443"; I want to use a PHP script to echo the most 3 repeated numbers in this string So, the result should be... 443 224 665 Do you know how can i do that in PHP Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/ Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 explode array_count_values Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728405 Share on other sites More sharing options...
Brian W Posted January 2, 2009 Share Posted January 2, 2009 Well, I don't know if Mchl's answer works for you, but I wanted to see if I could get the Mode (that what it is called in math) three times. Here it is <?php $string = array(224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443); function M_MODE($array){ ksort($array); $Sets = array(); foreach($array as $number){ if(in_array($number, $array)){ $Sets[$number]++; } } arsort($Sets); echo key($Sets)." "; next($Sets); echo key($Sets)." "; next($Sets); echo key($Sets)." "; } M_MODE($string); ?> It probably sucks but it works and I'm proud of it. lol Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728429 Share on other sites More sharing options...
npsari Posted January 2, 2009 Author Share Posted January 2, 2009 Hey, thank you for the answers Thank you much Brian You code is working fine here too Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728434 Share on other sites More sharing options...
Mchl Posted January 2, 2009 Share Posted January 2, 2009 Well... I'd go with something along these lines $string = "224 556 665 224 667 443 776 443 222 443 667 224 997 665 009 443 443"; $stringArray = explode(" ",$string); //convert string to array $countValues = array_count_values($stringArray); //count values in array arsort($countValues); //sort in descending order maintaining key=>value association $result = array_slice($countValues,0,3,TRUE); //get three first elements from $countValues print_r($result); Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728442 Share on other sites More sharing options...
Brian W Posted January 2, 2009 Share Posted January 2, 2009 Damn, all of what I did is nullified by one pre-existing function. Meh, live and learn. Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728444 Share on other sites More sharing options...
npsari Posted January 3, 2009 Author Share Posted January 3, 2009 Thank you both though, there is somethign weird here I changed a tiny bit in the first line <?php $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; $string = array($numbers); function M_MODE($array){ ksort($array); $Sets = array(); foreach($array as $number){ if(in_array($number, $array)){ $Sets[$number]++; } } arsort($Sets); echo key($Sets)." "; next($Sets); echo key($Sets)." "; next($Sets); echo key($Sets)." "; } M_MODE($string); ?> It doesnt work anymore, whyyy Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728505 Share on other sites More sharing options...
kenrbnsn Posted January 3, 2009 Share Posted January 3, 2009 The problem is that this <?php $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; $string = array($numbers); ?> is not the same as <?php $string = array(224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443); ?> The first code creates an array with one entry that is the string. The second one creates an array of all the numbers. Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728550 Share on other sites More sharing options...
npsari Posted January 3, 2009 Author Share Posted January 3, 2009 Thank you Ken Can you offer me a soultion, because i have the string $numbers (which contains all the numbers) Can you tell me how can i create the array from it Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728889 Share on other sites More sharing options...
kenrbnsn Posted January 3, 2009 Share Posted January 3, 2009 Use explode: <?php $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; $string = explode(',',$numbers); ?> Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728891 Share on other sites More sharing options...
npsari Posted January 3, 2009 Author Share Posted January 3, 2009 Thank you Ken Is it possible that you tell me how to do this, because i can't succeed I am trying to get the results as strings arsort($Sets); echo key($Sets)." "; next($Sets); $get1 = key($Sets)." "; next($Sets); $get2 = key($Sets)." "; Because i want to use $get1 & $get2 & $get3 later on What i done above doesn't work Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-728971 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2009 Share Posted January 4, 2009 The problem is that $get1, $get2 & $get3 are set in the function, so they are local to the function. To use them outside, you have to return the values at the end of the function. <?php function mode($set) { $cv = array_count_values($set); arsort($cv); $result = array_slice($cv,0,3,TRUE); print_r(array_keys($result)); return(array_keys($result)); } $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; list($get1,$get2,$get3) = mode(explode(',',$numbers)); echo $get1 . "<br>"; echo $get2 . "<br>"; echo $get3 . "<br>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729078 Share on other sites More sharing options...
npsari Posted January 4, 2009 Author Share Posted January 4, 2009 Is this a new way of doing it, because i am using the method provided by Brian Anyway, I uploaded your script in an empty php page, i got the following error... Warning: Wrong parameter count for array_slice() in /home/zortins/public_html/try.php on line 5 Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 6 Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 7 Is it possibe to show me how can i do it in Brian's script (Because i already added to it parts of mine) Can you tell me how can i have $get1 $get2 $get3 outside that function What i have now is this: $get_string = "224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; $string = explode(',',$get_string ); function M_MODE($array){ ksort($array); $Sets = array(); foreach($array as $number){ if(in_array($number, $array)){ $Sets[$number]++; } } arsort($Sets); echo key($Sets)." "; next($Sets); $get1 = key($Sets); next($Sets); $get2 = key($Sets); next($Sets); $get3 = key($Sets); } M_MODE($string); print "$get1 - $get2 - $get3"; Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729099 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2009 Share Posted January 4, 2009 What version of php are your running? My script works perfectly with PHP 5.2.5 With the other routine: <?php function M_MODE($array){ ksort($array); $Sets = array(); foreach($array as $number){ if(in_array($number, $array)){ $Sets[$number]++; } } arsort($Sets); $result = array_slice($Sets,0,3,TRUE); return(array_keys($result)); } $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; list($get1,$get2,$get3) = M_MODE(explode(',',$numbers)); echo $get1 . "<br>"; echo $get2 . "<br>"; echo $get3 . "<br>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729113 Share on other sites More sharing options...
npsari Posted January 4, 2009 Author Share Posted January 4, 2009 To tell you the truth, I dont know about php versions, all i know is PHP! I am a Mechanical Engineer, programming is a hobby to me Thanks for helping me by the way So anyway, i uploaded this new script you just given me in an empty page I got that error... Warning: Wrong parameter count for array_slice() in /home/zortins/public_html/try.php on line 11 Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/zortins/public_html/try.php on line 12 So, you took Brian's script and shaped it differently, is that what you have done here Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729116 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2009 Share Posted January 4, 2009 Remove the ",TRUE" from the array_slice arguments. It sounds like your version of PHP is earlier that 5.0.2. Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729125 Share on other sites More sharing options...
npsari Posted January 4, 2009 Author Share Posted January 4, 2009 Yes, i removed it just now, it worked ok with no errors However, the results were... 0 1 2 Any idea why this happened Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729128 Share on other sites More sharing options...
kenrbnsn Posted January 4, 2009 Share Posted January 4, 2009 change the routine to: <?php function M_MODE($array){ ksort($array); $Sets = array(); foreach($array as $number){ if(in_array($number, $array)){ $Sets[$number]++; } } arsort($Sets); $result = array_slice(array_keys($Sets),0,3); return($result); } $numbers="224,556,665,224,667,443,776,443,222,443,667,224,997,665,009,443,443"; list($get1,$get2,$get3) = M_MODE(explode(',',$numbers)); echo $get1 . "<br>"; echo $get2 . "<br>"; echo $get3 . "<br>"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/139252-what-are-the-most-3-repeated-numbers-in-a-string/#findComment-729166 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.