2013-07-10 5 views
0

나는 다음과 같은 배열이 : 나는이 같은 이벤트를 계산하려고계산 foreach 루프 항목

[2]=> 
    object(stdClass)#9 (4) { 
    ["ID"]=> 
    string(32) "43c845f895a56fbe8aea9435ef8fa806" 
    ["Type"]=> 
    string(8) "Campaign" 
    ["Name"]=> 
    string(28) "An unmissable invitation for" 
    ["Actions"]=> 
    array(5) { 
     [0]=> 
     object(stdClass)#10 (4) { 
     ["Event"]=> 
     string(4) "Open" 
     ["Date"]=> 
     string(19) "2013-05-07 17:00:00" 
     ["IPAddress"]=> 
     string(12) "109.239.93.2" 
     ["Detail"]=> 
     string(0) "" 
     } 
     [1]=> 
     object(stdClass)#11 (4) { 
     ["Event"]=> 
     string(4) "Open" 
     ["Date"]=> 
     string(19) "2013-05-07 09:01:00" 
     ["IPAddress"]=> 
     string(12) "109.239.93.2" 
     ["Detail"]=> 
     string(0) "" 
     } 
     [2]=> 
     object(stdClass)#12 (4) { 
     ["Event"]=> 
     string(4) "Open" 
     ["Date"]=> 
     string(19) "2013-04-30 22:29:00" 
     ["IPAddress"]=> 
     string(14) "94.171.192.216" 
     ["Detail"]=> 
     string(0) "" 
     } 
     [3]=> 
     object(stdClass)#13 (4) { 
     ["Event"]=> 
     string(5) "Click" 
     ["Date"]=> 
     string(19) "2013-04-30 17:43:00" 
     ["IPAddress"]=> 
     string(12) "109.239.93.2" 
     ["Detail"]=> 
     string(60) "http://www.rbh.co.uk/rbhevent/?name=[fullname]&email=[email]" 
     } 
     [4]=> 
     object(stdClass)#14 (4) { 
     ["Event"]=> 
     string(4) "Open" 
     ["Date"]=> 
     string(19) "2013-04-30 17:43:00" 
     ["IPAddress"]=> 
     string(12) "109.239.93.2" 
     ["Detail"]=> 
     string(0) "" 
     } 
    } 
    } 

합니다. 그래서 예를 들어 [ "Event"] = Open = 4/[ "Event"] = Click = 1.

$i=0; 
foreach($entry->Actions as $actions) { 
echo $i++;   
} 

임 꽤 확실하지가 어떻게 접근하는 :

은 내가 foreach 루프를 계산을 통해이를 달성하기 위해 노력하고 있어요? 누군가 모범 사례를 제안 할 수 있습니까?

+1

루프의 if 문을 사용 하시겠습니까? – Neal

+0

각 이벤트 값이 키이고 값이 카운트 인 맵을 유지하십시오. –

답변

5
$counts = array(); 

foreach($entry->Actions as $actions) { 
    if(!isset($counts[$actions->Event])) { 
     $counts[$actions->Event] = 0; 
    } 
    ++$counts[$actions->Event]; 
} 

print_r($counts); 
+0

감사합니다! 그래서 내가 $ counts [$ actions-> Event]를 echo했을 때; 나는 [ "Open"] 값에 대한 합계를 얻는다. [ "클릭"] 값의 합계를 얻으려면 어떻게해야합니까? – danyo

+1

'$ counts'는 배열이므로'$ counts [ 'Click']'을 echo 해보십시오. –

4
<?php 
$amounts = array(); // Events as key 
foreach($entry->Actions as $actions) 
{ 
    if (isset($amounts[$actions["Event"]])) $amounts[$actions["Event"]]++; 
    else $amounts[$actions["Event"]] = 1; 
} 
print_r($amounts); 
echo "<br>".$amounts["Open"]; 
?> 
+0

당신이 저를 때려 눕 힙니다. :) –

+0

수정을 제안합니다 'if (isset ($ actions [ "Event"]])) $ amount [$ actions [ "Event"]] ++; else $ amount [$ events [ "Event"]] = 1; ' – Trogvar

+0

감사합니다. –