2009-08-19 16 views
1

j'ai foreach, qui génèrent des tableaux suivants:nombre de plusieurs tableaux multidimensionnels

==== 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) 

Comme sortie je veux obtenir quelque chose comme:

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

La valeur du nouveau tableau est le nombre de l'élément de tout generatet Aray de la boucle. array_count_values ​​() ne fonctionne pas ici ... des suggestions, comment résoudre le problème?

+0

Y a-t-il une raison pour laquelle array1 et array2 sont bidimensionnels? – VolkerK

+0

oui, je veux plus tard développer les tableaux sous la forme 'test1' => tableau ('count' => '1', 'uri' => 'http: // testuri') – cupakob

Répondre

2

Quelque chose un peu comme cela devrait fonctionner:

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

    $result[$name]++; 
} 
4

n'a pas remarqué qu'il était 2 dimensions.

Voici un autre code.

var_export(
    array_count_values(
     call_user_func_array('array_merge', array_merge($array1, $array2)) 
    ) 
); 
+0

Nice. J'ai appris une nouvelle fonction. – easement

+0

Beauté, j'aime comment vous avez utilisé call_user_func_array() pour aplatir le tableau. – bucabay

2

Utilisons le Standard PHP Library (SPL).
Vous pouvez "aplatir" un tableau avec RecursiveArrayIterator et RecursiveIteratorIterator. Par conséquent, vous obtenez un itérateur qui visite chaque feuille de votre tableau à n dimensions et vous permet toujours d'accéder à la clé réelle de l'élément. Dans l'étape suivante, concattez les deux RecursiveIteratorIterators avec un AppendIterator agissant comme un seul interacteur qui visite chaque élément dans tous ses itérateurs internes (ajoutés).

$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; 
    } 
} 

Si vous voulez, vous pouvez même utiliser un FilterIterator au lieu du if('tag'===$key). Mais à mon avis ceci n'augmente pas la lisibilité/la valeur du code ;-)