PHP: Counting similar occurences in multidimensional array -
Hello!
I have a multi-dimensional array in which I want to calculate similar incidents.
So basically I need this:
[['type' = & gt; 'Frosties', 'madeby' => 'Klogs'], ['type' = & gt; 'Frosties', 'madeby' => 'Klogs'], ['type' = & gt; 'Cornflakes', 'made' = & gt; 'Kelogs']];
To end it:
[['type' = & gt; 'Frosties', 'madeby' => 'Kelloggs', 'Count' = & gt; 2], ['type' = & gt; 'Cornflakes', 'made' = & gt; 'Kelloggs', 'Count' = & gt; Public function count ($ array) {$ newArr = 1}
[]; Foreign currency ($ breakfast as breakfast) {if (in_array ($ breakfast ['type'], $ newArr) and amp; in_array ($ breakfast ['madeby'], $ newArr) is? // DoSomething ['Count'] ++; } And {$ newArr [] = ['type' = & gt; $ Breakfast ['type'], 'made' => $ Breakfast ['made'], 'count' => 0]; }} Return $ newArr; }
I could have been staring at it for a long time, but I think what happens if () goes inside, I do not think so.
Thanks guys in advance.
Here you go:
$ array = [['type '= & Gt; 'Frosties', 'madeby' => 'Klogs'], ['type' = & gt; 'Frosties', 'madeby' => 'Klogs'], ['type' = & gt; 'Cornflakes', 'made' = & gt; 'Kelogs']]; $ Result = []; Foreign Currency ($ couple dollar pairs) {// ksort ($ pair); & Lt; - It may be required here if the typed and ordered commands are not in the same order $ Key = Serialize ($ pair); If (isset ($ result [$ key])) {$ result [$ key] ['count'] ++; } And {$ result [$ key] = array_merge ($ pair, ['count' = & gt; 1]); }} $ Result = array_values ($ result); Print_r ($ result);
Comments
Post a Comment