1

i have foreach, which generate following arrays:

==== array 1 ====
array
  0 => 
    array
      'tag' => string 'daf' (length=3)
  1 => 
    array
      'tag' => string 'daa' (length=3)
  2 => 
    array
      'tag' => string 'daf' (length=3)
  3 => 
    array
      'tag' => string 'daaa' (length=4)
  4 => 
    array
      'tag' => string 'daf' (length=3)
  5 => 
    array
      'tag' => string 'daa' (length=3)
  6 => 
    array
      'tag' => string 'daf' (length=3)
  7 => 
    array
      'tag' => string 'daf' (length=3)
  8 => 
    array
      'tag' => string 'daf' (length=3)
  9 => 
    array
      'tag' => string 'abd' (length=3)
  10 => 
    array
      'tag' => string 'abdaa' (length=5)
  11 => 
    array
      'tag' => string 'abda' (length=4)

==== array 2 ====    
array
  0 => 
    array
      'tag' => string 'daf' (length=3)
  1 => 
    array
      'tag' => string 'test1' (length=5)

As output i want to get something like:

array
  'daf' => '7'
  'daa' => '2'
  'daaa' => '1'
  'abd' => '1'
  'abdaa' => '1'
  'abda' => '1'
  'test1' => '1'

The value of the new array is the count of the element from all aray generatet from the loop. array_count_values() doesn't work here...any suggestions, how to solve the problem?

2
  • Is there a reason why array1 and array2 are 2-dimensional? Commented Aug 19, 2009 at 13:10
  • yes, i want later to expand the arrays in the form 'test1' => array('count' => '1', 'uri' => 'testuri') Commented Aug 19, 2009 at 13:43

3 Answers 3

4

Did not notice it was 2 dimensional array.

Here is another code.

var_export(
    array_count_values(
        call_user_func_array('array_merge', array_merge($array1, $array2))
    )
);
Sign up to request clarification or add additional context in comments.

1 Comment

Beauty, I like how you used call_user_func_array() to flatten the array.
2

Something a bit like this should work:

$result = array();
foreach (array_merge($array1, $array2) as $item) {
    $name = $item['tag'];   
    if (!isset($result[$name])) {
        $result[$name] = 0;   
    } 

    $result[$name]++;
}

Comments

2

Let's make some use of the Standard PHP Library (SPL).
You can "flatten" an array with an RecursiveArrayIterator and RecursiveIteratorIterator. As a result you get an iterator that visits each leaf of your n-dimensional array and still let's you access the actual key of the element. In the next step concat both RecursiveIteratorIterators with an AppendIterator acting like a single interator that visits each element in all of its inner (appended) iterators.

$ai = new AppendIterator;
$ai->append(new RecursiveIteratorIterator(new RecursiveArrayIterator($array1)));
$ai->append(new RecursiveIteratorIterator(new RecursiveArrayIterator($array2)));
$counters = array();
foreach($ai as $key=>$value) {
  if ( 'tag'===$key ) {
    // @ because I don't care whether this array element exists beforehand or not.
    // $value has to be something that can be used as an array key (strings in this case)
    @$counters[$value] += 1;
  }
}

If you want you can even use a FilterIterator instead of the if('tag'===$key). But imho this doesn't increase the readability/value of the code ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.