Ninjakreborn Posted July 27, 2009 Share Posted July 27, 2009 I have an array with 10 items. Each one fo those items contains a master item and a subarray of subitems. I need to loop through EACH of the 10 arrays through all of the sub items and find out how many matches there are alike. So if sub item abc appears in array 1, and array 4 then I need to list out they were found 2 times. It's wierd. i am guessing I need to create a matching algorithm for the arrays. Where should I start? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 Something like this? <?php $array = array( array( 'foo', 'bar', ), array( 'foo', 'bar', 'baz', ), array( 'foo', 'baz', ), ); // foo = 3 // bar = 2 // baz = 2 $count = array(); foreach ($array as $items) { foreach ($items as $item) { if (!isset($count[$item])) { $count[$item] = 1; } else { $count[$item]++; } } } print_r($count); Output: Array ( [foo] => 3 [bar] => 2 [baz] => 2 ) Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 27, 2009 Author Share Posted July 27, 2009 Very similiar. I ended up working up something simple to avoid the problem, so I got that situated. The next question I have is how to sort an array based on the number of values that array has. So let's say I have a big array. bigarray['a']['a'] = 'test' bigarray['a']['b'] = 'test' bigarray['a']['c'] = 'test' bigarray['b']['a'] = 'test' bigarray['b']['b'] = 'test' bigarray['c']['a'] = 'test' I don't know what order that array would be in so I want to sort it so I can see the ones with the "most" values first. bigarray['a'] would be first then bigarray['b'] then bigarray['c'] I have a huge array and they are similiar, I just want to order the primary array based off the amount of each values. So if bigarray[c] had more values than a it would appear first. Then I will be listing them out based off how many values were in each array. Does that make sense? Any advice? Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 Something like this should do: uasort($bigarray, create_function('$a,$b', 'return count($a) > count($b);')); usort, uasort Quote Link to comment Share on other sites More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 <?php array_multisort($bigarray, SORT_DESC); ?> Just a simple multisort will do the job. <?php $bigarray['a']['a'] = 'test'; $bigarray['a']['b'] = 'test'; $bigarray['b']['a'] = 'test'; $bigarray['b']['b'] = 'test'; $bigarray['b']['c'] = 'test'; $bigarray['c']['a'] = 'test'; array_multisort($bigarray, SORT_DESC); var_dump($bigarray); ?> array(3) { ["b"]=> array(3) { ["a"]=> string(4) "test" ["b"]=> string(4) "test" ["c"]=> string(4) "test" } ["a"]=> array(2) { ["a"]=> string(4) "test" ["b"]=> string(4) "test" } ["c"]=> array(1) { ["a"]=> string(4) "test" } } Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted July 27, 2009 Author Share Posted July 27, 2009 Thanks, I appreciate that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.