How to remove duplicate values from an array in PHP and count the appearance of each element? I have this array
I want the result to be in an array like this
value freq ---- ---- foo 2 bar 1
thanks
so simple php have function
$a=array("Cat","Dog","Horse","Dog"); print_r(array_count_values($a));
Code output above:
Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )
You want array_count_values () and then array_unique () .
$arr = array('foo','bar','foo'); print_r(array_count_values($arr)); $arr = array_unique($arr); print_r($arr);
gives:
Array ( [foo] => 2 [bar] => 1 ) Array ( [0] => foo [1] => bar )
To remove duplicates from a table, use array_unique () .
Something like this is possible (unverified)
$freq = array(); foreach($arr as $val) { $freq[$val] = 0; } foreach($arr as $val) { $freq[$val]++; }
Source: https://habr.com/ru/post/1714375/More articles:General programming - one way or another, if for clarity - if-statementJsp error when using if-else with html - javaСоветы для разработки Delphi на Mac? - delphiJavascript: Enter a keyword - javascriptHow to refuse a long-term search in System.DirectoryServices.Protocols - searchArtificial Intelligence - Creative Writing - c #How to save $ .getJSON return value in jQuery - jquerySQL Server добавляет агент задания T-SQL script issue - sqlFacebook wall - facebookHow to add IOperationInvoker to a wcf client - c #All Articles