Does anyone know how to count the occurrences of "photo" in this array:
有谁知道如何计算此数组中“photo”的出现次数:
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 ) )
4 个解决方案
#1
14
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
#2
55
To count matching occurance of a string in multidimensional array you will need to iterate over each array element and match the string and increment the count. Similiarly @Dor has suggested
要计算多维数组中字符串的匹配出现次数,您需要迭代每个数组元素并匹配字符串并递增计数。同样@Dor建议
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
If you want achieve same in single dimensional array then It's pretty straightforward. You can use array_count_values PHP array function as explained below.
如果你想在单维数组中实现相同,那么它非常简单。您可以使用array_count_values PHP数组函数,如下所述。
<?php
$array = array(1, "test", 1, "php", "test");
print_r(array_count_values($array));
?>
The above example will output:
以上示例将输出:
Array
(
[1] => 2
[test] => 2
[php] => 1
)
#3
2
I'd like to acknowledge that the method by Dor Shemer is (IMO) the most direct, clean, readable, and reliable method. I just want to offer a few alternatives for those who prefer to use functional programming ...array_reduce()
is a close second for me. Finally, I want to pinpoint a small gotcha for methods that use array_count_values()
-- please read on...
我想承认Dor Shemer的方法是(IMO)最直接,最简洁,最易读,最可靠的方法。我只是想为那些喜欢使用函数式编程的人提供一些替代方案... array_reduce()对我来说是紧随其后的。最后,我想针对使用array_count_values()的方法确定一个小问题 - 请继续阅读...
Battery of Methods: (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'];
Input/Output using OP's sample data (no trouble):
使用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
Input/Output using data with no photo
values (trouble with 2nd array_count_values()
method):
输入/输出使用没有照片值的数据(第二个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()
doesn't bother to generate elements with a 0
count.
array_count_values()不会生成具有0计数的元素。
#4
1
Try with:
试试:
$input = array( /* your data */ );
$count = 0;
foreach ( $input as $value ) {
if ( $value->type == 'photo' ) {
$count++;
}
}
#1
14
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
#2
55
To count matching occurance of a string in multidimensional array you will need to iterate over each array element and match the string and increment the count. Similiarly @Dor has suggested
要计算多维数组中字符串的匹配出现次数,您需要迭代每个数组元素并匹配字符串并递增计数。同样@Dor建议
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
If you want achieve same in single dimensional array then It's pretty straightforward. You can use array_count_values PHP array function as explained below.
如果你想在单维数组中实现相同,那么它非常简单。您可以使用array_count_values PHP数组函数,如下所述。
<?php
$array = array(1, "test", 1, "php", "test");
print_r(array_count_values($array));
?>
The above example will output:
以上示例将输出:
Array
(
[1] => 2
[test] => 2
[php] => 1
)
#3
2
I'd like to acknowledge that the method by Dor Shemer is (IMO) the most direct, clean, readable, and reliable method. I just want to offer a few alternatives for those who prefer to use functional programming ...array_reduce()
is a close second for me. Finally, I want to pinpoint a small gotcha for methods that use array_count_values()
-- please read on...
我想承认Dor Shemer的方法是(IMO)最直接,最简洁,最易读,最可靠的方法。我只是想为那些喜欢使用函数式编程的人提供一些替代方案... array_reduce()对我来说是紧随其后的。最后,我想针对使用array_count_values()的方法确定一个小问题 - 请继续阅读...
Battery of Methods: (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'];
Input/Output using OP's sample data (no trouble):
使用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
Input/Output using data with no photo
values (trouble with 2nd array_count_values()
method):
输入/输出使用没有照片值的数据(第二个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()
doesn't bother to generate elements with a 0
count.
array_count_values()不会生成具有0计数的元素。
#4
1
Try with:
试试:
$input = array( /* your data */ );
$count = 0;
foreach ( $input as $value ) {
if ( $value->type == 'photo' ) {
$count++;
}
}