web-dev-qa-db-ja.com

オブジェクト配列の配列の列にある値の出現をカウントする

この配列内の「写真」の出現を数える方法を誰かが知っていますか:

    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 ) )
20
G.W
$count = 0;
foreach ($array as $item) {
  if ($item->type === 'photo') {
    $count++;
  }
}
15
Dor Shemer

多次元配列内の文字列の一致発生をカウントするには、各配列要素を反復処理して文字列を一致させ、カウントをインクリメントする必要があります。同様に@Dorが提案しました

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

一次元配列で同じことを実現したい場合は、かなり簡単です。以下で説明するように、 array_count_values PHP array関数を使用できます。

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

上記の例は出力します:

Array
(
    [1] => 2
    [test] => 2
    [php] => 1
)
56
Roopendra

私は、Dor Shemerによる方法が(IMO)最も直接的で、クリーンで、読みやすく、信頼できる方法であることを認めたいと思います。私は関数型プログラミングを好む人のためにいくつかの代替案を提供したいだけです...array_reduce()は私にとってもうすぐです。最後に、array_count_values()を使用するメソッドの小さな問題を特定したいと思います-読んでください...

一連のメソッド:( デモ

_$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'];
_

OPのサンプルデータを使用した入出力(問題なし):

_$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値のないデータを使用した入出力(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_カウントの要素を生成する必要はありません。

2
mickmackusa

試してください:

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