20

有誰知道如何在這個數組中計數的「照片」的出現:計數出現

Array ( 
    [0] => stdClass Object ([type] => photo [id] => 1404781893036 [created_time] => 2012-03-02T07:58:23+0000) 
    [1] => stdClass Object ([type] => photo [id] => 14047818930362 [created_time] => 2012-03-01T14:58:53+0000) 
    [2] => stdClass Object ([type] => status [id] => 1404781893036 [created_time] => 2012-03-01T09:49:40+0000) 
    [3] => stdClass Object ([type] => status [id] => 14047818930362 [created_time] => 2012-03-01T09:36:04+0000) 
    [4] => stdClass Object ([type] => photo [id] => 14047818930362 [created_time] => 2012-02-28T07:03:25+0000) 
    [5] => stdClass Object ([type] => photo [id] => 1404781893036 [created_time] => 2012-02-27T09:15:34+0000) 
    [6] => stdClass Object ([type] => photo [id] => 14047818930362 [created_time] => 2012-02-27T07:32:13+0000) 
    [7] => stdClass Object ([type] => status [id] => 1404781893036 [created_time] => 2012-02-25T09:36:57+0000) 
    [8] => stdClass Object ([type] => photo [id] => 1404781893036 [created_time] => 2012-02-23T08:46:43+0000) 
    [9] => stdClass Object ([type] => status [id] => 1404781893036 [created_time] => 2012-02-22T21:04:30+0000) 
    [10] => stdClass Object ([type] => status [id] => 1404781893036 [created_time] => 2012-02-21T20:38:27+0000) 
    [11] => stdClass Object ([type] => photo [id] => 1404781893036 [created_time] => 2012-02-21T07:22:44+0000) 
    [12] => stdClass Object ([type] => status [id] => 14047818930362 [created_time] => 2012-02-20T08:32:46+0000) 
    [13] => stdClass Object ([type] => status [id] => 1404781893036 [created_time] => 2012-02-17T15:00:11+0000)) 

回答

13
$count = 0; 
foreach ($array as $item) { 
    if ($item->type === 'photo') { 
    $count++; 
    } 
} 
1

與嘗試:

$input = array(/* your data */); 
$count = 0; 
foreach ($input as $value) { 
    if ($value->type == 'photo') { 
    $count++; 
    } 
} 
55

要計算多維數組中字符串匹配的發生次數,您需要遍歷每個數組元素並匹配字符串並遞增計數。同理@Dor建議

$count = 0; 
foreach ($array as $item) { 
    if ($item->type === 'photo') { 
    $count++; 
    } 
} 

如果你想要在一維數組中實現相同,那麼它非常簡單。您可以使用array_count_values PHP數組函數,如下所述。

<?php 
    $array = array(1, "test", 1, "php", "test"); 
    print_r(array_count_values($array)); 
?> 

上例將輸出:

Array 
(
    [1] => 2 
    [test] => 2 
    [php] => 1 
) 
+0

遍歷數組,並在遇到type ='photo'時增加ctrPhoto – 2012-11-20 10:15:25

+2

這應該是正確的答案,因爲它更簡單並且避免手動迭代 – JDuarteDJ 2013-07-05 12:09:06

+2

這個答案不是特定於OP的問題,而Dor Shemer專門爲他回答這個問題,爲什麼他是被接受的答案。無論如何,我現在搜索SOF的答案就是這個+1。 – Jacksonkr 2015-09-30 16:18:26

2

我想確認由多爾沙麥該方法是(IMO)的最直接的,乾淨的,可讀的,且可靠的方法。我只想爲那些更喜歡使用函數式編程的人提供一些選擇... array_reduce()對我來說是緊隨其後的。最後,我要找出需要使用array_count_values()方法小疑難雜症 - 請繼續閱讀...

方法的電池:(Demo

使用OP的樣本數據
$photo_count=0; // establish default value 
foreach($array as $objects){ 
    if($objects->type==='photo') ++$photo_count; // pre-increment 
} 
echo "foreach result = $photo_count"; 

echo "array_reduce = ",array_reduce($array,function($carry,$objects){return $carry+($objects->type==='photo'?1:0);},0); 

echo "array_filter & count = ",sizeof(array_filter($array,function($objects){return $objects->type==='photo';})); 

echo "array_column & array_filter & count = ",sizeof(array_filter(array_column($array,'type'),function($v){return $v==='photo';})); 

echo "array_map & array_count_values & array_replace = ",array_replace(['photo'=>0],array_count_values(array_map(function($o) {return $o->type;}, $array)))['photo']; 

echo "array_map & array_count_values (gives Notice) = ",array_count_values(array_map(function($o) {return $o->type;}, $array))['photo']; 

輸入/輸出(無煩惱):

$array=[ 
    (object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-03-02T07:58:23+0000'], 
    (object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-03-01T14:58:53+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'], 
    (object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'], 
    (object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-28T07:03:25+0000'], 
    (object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-27T09:15:34+0000'], 
    (object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-27T07:32:13+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'], 
    (object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-23T08:46:43+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'], 
    (object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-21T07:22:44+0000'], 
    (object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000'] 
]; 

// output: 
foreach result = 7 

array_reduce = 7 

array_filter & count = 7 

array_column & array_filter & count = 7 

array_map & array_count_values & array_replace = 7 

array_map & array_count_values = 7 

輸入/輸出使用帶有無photo值的數據(與第二array_count_values()方法麻煩):

$array=[ 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'], 
    (object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'], 
    (object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'], 
    (object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000'] 
]; 
// or if there are no object rows like: $array=[]; 
// output: 
foreach result = 0 

array_reduce = 0 

array_filter & count = 0 

array_column & array_filter & count = 0 

array_map & array_count_values & array_replace = 0 

array_map & array_count_values (gives Notice) = <br /> 
<b>Notice</b>: Undefined index: photo in <b>[...][...]</b> on line <b>43</b><br /> 

array_count_values()不打算生成0計數的元素。