20

Có ai biết làm thế nào để đếm số lần xuất hiện của "ảnh" trong mảng này:lần xuất hiện Tính của một giá trị trong một cột của một mảng của mảng đối tượng

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)) 

Trả lời

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

Hãy thử với:

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

Để đếm số lần xuất hiện phù hợp của một chuỗi trong mảng đa chiều, bạn sẽ cần phải lặp qua từng phần tử mảng và khớp với chuỗi và tăng số lượng. Tương tự @Dor đã đề xuất

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

Nếu bạn muốn đạt được cùng trong mảng một chiều thì điều đó khá đơn giản. Bạn có thể sử dụng chức năng mảng array_count_values PHP như được giải thích bên dưới.

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

Ví dụ trên sẽ ra:

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

Lặp lại mảng và tăng ctrPhoto bất cứ khi nào loại = 'ảnh' gặp phải –

+2

Đây là câu trả lời đúng bởi vì nó đơn giản hơn và tránh lặp lại thủ công – JDuarteDJ

+2

Câu trả lời này không dành riêng cho câu hỏi của OP trong khi Dor Shemer đặc biệt phục vụ trả lời cho câu hỏi vì thế tại sao anh ta lại là câu trả lời được chấp nhận. Bất kể, câu trả lời cho lý do tại sao tôi đang tìm kiếm trên SOF ngay bây giờ là cái này để +1. – Jacksonkr

2

tôi muốn thừa nhận rằng phương pháp của Dor Shemer là (IMO) phương pháp trực tiếp nhất, sạch sẽ, dễ đọc, và đáng tin cậy. Tôi chỉ muốn cung cấp một vài lựa chọn thay thế cho những người thích sử dụng lập trình chức năng ... array_reduce() là một giây gần gũi đối với tôi. Cuối cùng, tôi muốn xác định một Gotcha nhỏ so với phương pháp sử dụng array_count_values() - vui lòng đọc trên ...

Pin của Phương pháp: (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 sử dụng dữ liệu mẫu OP của (không gặp khó khăn):

$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 sử dụng dữ liệu không có photo giá trị (rắc rối với phương pháp thứ 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() không bận tâm khi tạo các phần tử có số lượng 0.

Các vấn đề liên quan