2016-06-20 15 views
-1

Ich habe ein Array und ich muss die Schlüssel mit einem bestimmten Wert zählen, was sich als Albtraum erweist.Zähle mehrdimensionale Array-Schlüssel mit spezifischem Wert

Array([0] => Array 
    (
     [0] => 1 
     [ruleid] => 1 
     [1] => Test Outbound Life 1 
     [rule_name] => Test Outbound Life 1 
     [2] => Life Insurance 
     [product_type] => Life Insurance 
     [3] => 1 
     [status] => 1 
     [4] => 1000 
     [priority] => 1000 
     [5] => 100 
     [quantity] => 100 
     [6] => 1-2-3-4-5-6-7- 
     [dayofweek] => 1-2-3-4-5-6-7- 
     [7] => 2 
     [income] => 2 
     [8] => external/arc.php 
     [integrationfile] => external/arc.php 
     [9] => 1 
     [partnerid] => 1 
    ) 

[1] => Array 
    (
     [0] => 2 
     [ruleid] => 2 
     [1] => Test Outbound Life 2 
     [rule_name] => Test Outbound Life 2 
     [2] => Life Insurance 
     [product_type] => Life Insurance 
     [3] => 1 
     [status] => 1 
     [4] => 800 
     [priority] => 800 
     [5] => 100 
     [quantity] => 100 
     [6] => 1-2-3-4-5-6-7- 
     [dayofweek] => 1-2-3-4-5-6-7- 
     [7] => 2 
     [income] => 2 
     [8] => test.php 
     [integrationfile] => test.php 
     [9] => 1 
     [partnerid] => 1 
    )) 

Das Array wird dynamisch generiert, sodass dasselbe Array im Array angezeigt wird.

Ich möchte zählen, wie oft die gleiche ruleid erscheint, ist es wie folgt aussehen:

Array{ 
    [1] => 1 
    [2] => 1 
} 

Update: Ich muss zählen, wie oft ruleid = 2 oder wie oft ruleid = 1

+1

'array_count_values ​​(array_column ($ yourArray, 'ruleid'))' sollte es tun – billyonecan

Antwort

0

Sie möchten also die Anzahl der Zeit, die jedes ruleid in einem Array angezeigt wird, zählen. Nennen wir dieses Array $count. So würde ich es machen.

$count = array(); 
foreach($arrays as $array) { // $arrays is your big ass array containing arrays 
    // increment the value with the key corresponding to ruleid (improved by JustOnUnderMillions) 
    $count[$array['ruleid']] = isset($count[$array['ruleid']]) ? ($count[$array['ruleid']] + 1) : 1; 
} 
print_r(count); // should give you what you're looking for 
+2

Besser tun '$ zählen [$ array ['ruleid']] = isset ($ zähl [$ array ['ruleid']]) ? $ count [$ array ['ruleid']] + 1: 1; 'Der Schlüssel wird zum ersten Mal auf' NULL' gesetzt. PHP 5.6 - ignoriere dies !! – JustOnUnderMillions

+0

Entschuldigung, etwas verpasst hier ....... vergessen Sie es – JustOnUnderMillions

+0

Sie haben absolut Recht. – Dorian

0

Ich habe es durch Schleifen und Zählen herausgefunden, wie oft eine Regelid in einem Array erscheint.

Die Schleife prüft, ob die Regelid alle Regeln, die ich bereits gezählt hatte, in der Variable $ count übergeben hat.

Verwandte Themen