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가 제안했습니다

단일 차원 배열을 동일하게 구현하려면 매우 간단합니다. 아래에 설명 된대로 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를 증가시킵니다. –

+2

더 간단하고 수동 반복을 피하기 때문에 정답 일 수 있습니다. – JDuarteDJ

+2

Dor Shemer가 자신의 요리를 제공하는 반면이 답변은 OP의 질문과 관련이 없습니다. 그러므로 왜 그 사람이 받아 들여지는 대답인지에 대한 질문에 대답하십시오. 어쨌든, 지금 SOF에서 검색하는 이유에 대한 대답은 +1입니다. – Jacksonkr

2

내가 도르 세멜에 의한 방법 (IMO)임을 인정하고자하는 가장 직접적인 깨끗하고 읽을 수있는, 그리고 신뢰할 수있는 방법. 함수형 프로그래밍을 선호하는 사람들을 위해 몇 가지 대안을 제시하고자합니다 ... array_reduce()은 제게는 제 2의 길입니다. (Demo)

영업 이익의 샘플 데이터를 사용하여
$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_count_values()를 사용하는 방법에 대한 작은 잡았다을 파악하지하려면) : 아니오 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 

입력/출력 데이터를 이용하여 (2 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 개수의 요소를 생성하지 않아도됩니다.