(→PHP) |
|||
| 15번째 줄: | 15번째 줄: | ||
[[category: PHP]] | [[category: PHP]] | ||
{{참고|PHP array_count_value()}} | {{참고|PHP array_count_value()}} | ||
<syntaxhighlight lang='PHP'> | <syntaxhighlight lang='PHP' run> | ||
function array_count_value($arr, $val) { | function array_count_value($arr, $val) { | ||
return count(array_filter($arr, function($v) use($val) {return $v==$val;})); | return count(array_filter($arr, function($v) use($val) {return $v==$val;})); | ||
} | } | ||
$fruits = | $fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Orange']; | ||
echo array_count_value($fruits, 'Orange'); | echo array_count_value($fruits, 'Orange'); # 3 | ||
# 3 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang='PHP'> | <syntaxhighlight lang='PHP' run> | ||
$fruits = | $fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Orange']; | ||
$acvs = array_count_values($fruits); | $acvs = array_count_values($fruits); | ||
echo $acvs['Orange']; | echo $acvs['Orange']; # 3 | ||
# 3 | |||
</syntaxhighlight> | </syntaxhighlight> | ||
2021년 4월 14일 (수) 19:10 판
- 다른 뜻에 대해서는 array length 문서를 참조하십시오.
- 다른 뜻에 대해서는 PHP count() 문서를 참조하십시오.
- count()
- array_count_value()
1 POSIX Shell
Bash
Copy
FRUITS="Banana Orange Apple Orange Orange "
echo $FRUITS | tr ' ' '\n' | grep -c 'Orange' # 3
Loading
2 PHP
PHP
Copy
function array_count_value($arr, $val) {
return count(array_filter($arr, function($v) use($val) {return $v==$val;}));
}
$fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Orange'];
echo array_count_value($fruits, 'Orange'); # 3
Loading
PHP
Copy
$fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Orange'];
$acvs = array_count_values($fruits);
echo $acvs['Orange']; # 3
Loading
3 Python
Python
Copy
fruits = ['Banana', 'Orange', 'Apple', 'Orange', 'Orange']
print( fruits.count('Orange') )
# 3
4 Perl
Perl
Copy
my @fruits = ('Banana', 'Orange', 'Apple', 'Orange', 'Orange');
print $k = grep { $_ eq 'Orange' } @fruits;
# 3
5 SQL
5.1 MySQL
Console
Copy
mysql> SELECT * FROM fruits;
+--------+
| value |
+--------+
| Banana |
| Orange |
| Apple |
| Orange |
| Orange |
+--------+
5 rows in set (0.00 sec)
Console
Copy
mysql> SELECT COUNT(*) FROM fruits WHERE value='Orange';
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)