Php подсчет значений массива

Php подсчет значений массива

  • PHP Tutorial
  • PHP | Introduction
  • PHP Full Form
  • How to set PHP development environment in windows ?
  • LAMP installation and important PHP configurations on Ubuntu
  • PHP | Coding Standards
  • PHP | Basic Syntax
  • PHP | Variables
  • PHP echo and print
  • PHP | Data Types
  • PHP | Strings
  • PHP | Constants
  • PHP | Magic Constants
  • PHP | Decision Making
  • PHP | Loops
  • PHP | Superglobals
  • PHP | Regular Expressions
  • PHP Examples
  • Describe PHP Include and Require
  • PHP | Basics of File Handling
  • PHP | fopen( ) (Function open file or URL)
  • PHP fread( ) Function
  • PHP fclose( ) Function
  • PHP | fwrite( ) Function
  • PHP | Uploading File
  • PHP Cookies
  • PHP | Sessions
  • PHP Filter and Filter Constant
  • Implementing callback in PHP
  • Exception Handling in PHP
  • PHP | Arrays
  • PHP array() Function
  • PHP | array_change_key_case() Function
  • PHP array_chunk() Function
  • PHP | array_column() Function
  • PHP array_combine() Function
  • PHP array_count_values() Function
  • PHP array_diff() function
  • PHP array_diff_assoc() Function
  • PHP array_diff_key() Function
  • PHP array_diff_uassoc() Function
  • PHP array_diff_ukey() Function
  • PHP array_fill() function
  • PHP array_fill_keys() Function
  • PHP Array Functions Complete Reference
  • PHP Date and Time
  • PHP | checkdate() Function
  • PHP | date_create(), date_format(), date_add() Functions
  • PHP | date_create_from_format() Function
  • PHP | date_date_set() Function
  • PHP | date_default_timezone_get() Function
  • PHP | date_default_timezone_set() Function
  • PHP | date_diff() Function
  • PHP | date_get_last_errors() Function
  • PHP | date_isodate_set() Function
  • PHP | date_modify() Function
  • PHP | date_offset_get() Function
  • PHP | date_parse_from_format() Function
  • PHP | date_parse() Function
  • PHP | date_sub() Function
  • PHP basename( ) Function
  • PHP chgrp( ) Function
  • PHP chmod( ) Function
  • PHP chown( ) Function
  • PHP copy( ) Function
  • PHP dirname( ) Function
  • PHP disk_free_space( ) Function
  • PHP disk_total_space( ) Function
  • PHP feof( ) Function
  • PHP fflush( ) Function
  • PHP | fgetc( ) Function
  • PHP | fgets( ) Function
  • PHP | fgetss( ) Function
  • PHP | file_exists( ) Function
  • PHP file_get_contents() Function
  • PHP Filesystem Functions Complete Reference
  • PHP | ftp_alloc() function
  • PHP | ftp_chdir() function
  • PHP | ftp_close() function
  • PHP | ftp_connect() function
  • PHP | ftp_delete() function
  • PHP | ftp_exec() function
  • PHP | ftp_get() function
  • PHP | ftp_put() function
  • PHP | ftp_get_option() function
  • PHP | ftp_login() function
  • PHP | ftp_mdtm() Function
  • PHP | ftp_mkdir() function
  • PHP | Ds\Map allocate() Function
  • PHP | Ds\Map apply() Function
  • PHP | Ds\Map capacity() Function
  • PHP | DS\Map clear() Function
  • PHP | Ds\Map __construct() Function
  • PHP Ds\Map copy() Function
  • PHP | Ds\Map count() Function
  • PHP | Ds\Map diff() Function
  • PHP | Ds\Map filter() Function
  • PHP | Ds\Map first() Function
  • PHP Ds\Map Functions Complete Reference
  • SplDoublyLinkedList in PHP
  • PHP SplDoublyLinkedList add() Function
  • PHP SplDoublyLinkedList bottom() Function
  • PHP SplDoublyLinkedList count() function
  • PHP SplFixedArray count() Function
  • PHP SplFixedArray current() Function
  • PHP SplFixedArray getSize() Function
  • PHP SplFixedArray key() Function
  • PHP SplObjectStorage addAll() Function
  • PHP SplObjectStorage attach() Function
  • PHP SplObjectStorage contains() Function
  • PHP SplObjectStorage count() Function
  • PHP SplQueue::__construct() Function
  • PHP SPL Data structures Complete Reference
  • PHP Tutorial
  • PHP | Introduction
  • PHP Full Form
  • How to set PHP development environment in windows ?
  • LAMP installation and important PHP configurations on Ubuntu
  • PHP | Coding Standards
  • PHP | Basic Syntax
  • PHP | Variables
  • PHP echo and print
  • PHP | Data Types
  • PHP | Strings
  • PHP | Constants
  • PHP | Magic Constants
  • PHP | Decision Making
  • PHP | Loops
  • PHP | Superglobals
  • PHP | Regular Expressions
  • PHP Examples
  • Describe PHP Include and Require
  • PHP | Basics of File Handling
  • PHP | fopen( ) (Function open file or URL)
  • PHP fread( ) Function
  • PHP fclose( ) Function
  • PHP | fwrite( ) Function
  • PHP | Uploading File
  • PHP Cookies
  • PHP | Sessions
  • PHP Filter and Filter Constant
  • Implementing callback in PHP
  • Exception Handling in PHP
  • PHP | Arrays
  • PHP array() Function
  • PHP | array_change_key_case() Function
  • PHP array_chunk() Function
  • PHP | array_column() Function
  • PHP array_combine() Function
  • PHP array_count_values() Function
  • PHP array_diff() function
  • PHP array_diff_assoc() Function
  • PHP array_diff_key() Function
  • PHP array_diff_uassoc() Function
  • PHP array_diff_ukey() Function
  • PHP array_fill() function
  • PHP array_fill_keys() Function
  • PHP Array Functions Complete Reference
  • PHP Date and Time
  • PHP | checkdate() Function
  • PHP | date_create(), date_format(), date_add() Functions
  • PHP | date_create_from_format() Function
  • PHP | date_date_set() Function
  • PHP | date_default_timezone_get() Function
  • PHP | date_default_timezone_set() Function
  • PHP | date_diff() Function
  • PHP | date_get_last_errors() Function
  • PHP | date_isodate_set() Function
  • PHP | date_modify() Function
  • PHP | date_offset_get() Function
  • PHP | date_parse_from_format() Function
  • PHP | date_parse() Function
  • PHP | date_sub() Function
  • PHP basename( ) Function
  • PHP chgrp( ) Function
  • PHP chmod( ) Function
  • PHP chown( ) Function
  • PHP copy( ) Function
  • PHP dirname( ) Function
  • PHP disk_free_space( ) Function
  • PHP disk_total_space( ) Function
  • PHP feof( ) Function
  • PHP fflush( ) Function
  • PHP | fgetc( ) Function
  • PHP | fgets( ) Function
  • PHP | fgetss( ) Function
  • PHP | file_exists( ) Function
  • PHP file_get_contents() Function
  • PHP Filesystem Functions Complete Reference
  • PHP | ftp_alloc() function
  • PHP | ftp_chdir() function
  • PHP | ftp_close() function
  • PHP | ftp_connect() function
  • PHP | ftp_delete() function
  • PHP | ftp_exec() function
  • PHP | ftp_get() function
  • PHP | ftp_put() function
  • PHP | ftp_get_option() function
  • PHP | ftp_login() function
  • PHP | ftp_mdtm() Function
  • PHP | ftp_mkdir() function
  • PHP | Ds\Map allocate() Function
  • PHP | Ds\Map apply() Function
  • PHP | Ds\Map capacity() Function
  • PHP | DS\Map clear() Function
  • PHP | Ds\Map __construct() Function
  • PHP Ds\Map copy() Function
  • PHP | Ds\Map count() Function
  • PHP | Ds\Map diff() Function
  • PHP | Ds\Map filter() Function
  • PHP | Ds\Map first() Function
  • PHP Ds\Map Functions Complete Reference
  • SplDoublyLinkedList in PHP
  • PHP SplDoublyLinkedList add() Function
  • PHP SplDoublyLinkedList bottom() Function
  • PHP SplDoublyLinkedList count() function
  • PHP SplFixedArray count() Function
  • PHP SplFixedArray current() Function
  • PHP SplFixedArray getSize() Function
  • PHP SplFixedArray key() Function
  • PHP SplObjectStorage addAll() Function
  • PHP SplObjectStorage attach() Function
  • PHP SplObjectStorage contains() Function
  • PHP SplObjectStorage count() Function
  • PHP SplQueue::__construct() Function
  • PHP SPL Data structures Complete Reference
Читайте также:  Php prepared statements mysqli

Источник

array_count_values

array_count_values() returns an array using the values of array (which must be int s or string s) as keys and their frequency in array as values.

Parameters

The array of values to count

Return Values

Returns an associative array of values from array as keys and their count as value.

Errors/Exceptions

Throws E_WARNING for every element which is not string or int .

Examples

Example #1 array_count_values() example

The above example will output:

Array ( [1] => 2 [hello] => 2 [world] => 1 )

See Also

  • count() — Counts all elements in an array or in a Countable object
  • array_unique() — Removes duplicate values from an array
  • array_values() — Return all the values of an array
  • count_chars() — Return information about characters used in a string

User Contributed Notes 7 notes

Simple way to find number of items with specific values in multidimensional array:

$list = [
[ ‘id’ => 1 , ‘userId’ => 5 ],
[ ‘id’ => 2 , ‘userId’ => 5 ],
[ ‘id’ => 3 , ‘userId’ => 6 ],
];
$userId = 5 ;

echo array_count_values ( array_column ( $list , ‘userId’ ))[ $userId ]; // outputs: 2
?>

Here is a Version with one or more arrays, which have similar values in it:
Use $lower=true/false to ignore/set case Sensitiv.

$ar1 [] = array( «red» , «green» , «yellow» , «blue» );
$ar1 [] = array( «green» , «yellow» , «brown» , «red» , «white» , «yellow» );
$ar1 [] = array( «red» , «green» , «brown» , «blue» , «black» , «yellow» );
#$ar1= array(«red»,»green»,»brown»,»blue»,»black»,»red»,»green»); // Possible with one or multiple Array

$res = array_icount_values ( $ar1 );
print_r ( $res );

function array_icount_values ( $arr , $lower = true ) <
$arr2 =array();
if(! is_array ( $arr [ ‘0’ ])) < $arr =array( $arr );>
foreach( $arr as $k => $v ) <
foreach( $v as $v2 ) <
if( $lower == true ) < $v2 = strtolower ( $v2 );>
if(!isset( $arr2 [ $v2 ])) <
$arr2 [ $v2 ]= 1 ;
>else <
$arr2 [ $v2 ]++;
>
>
>
return $arr2 ;
>
/*
Will print:
Array
(
[red] => 3
[green] => 3
[yellow] => 4
[blue] => 2
[brown] => 2
[white] => 1
[black] => 1
)
*/
?>

I couldn’t find a function for counting the values with case-insensitive matching, so I wrote a quick and dirty solution myself:

function array_icount_values ( $array ) $ret_array = array();
foreach( $array as $value ) foreach( $ret_array as $key2 => $value2 ) if( strtolower ( $key2 ) == strtolower ( $value )) $ret_array [ $key2 ]++;
continue 2 ;
>
>
$ret_array [ $value ] = 1 ;
>
return $ret_array ;
>

$ar = array( ‘J. Karjalainen’ , ‘J. Karjalainen’ , 60 , ’60’ , ‘J. Karjalainen’ , ‘j. karjalainen’ , ‘Fastway’ , ‘FASTWAY’ , ‘Fastway’ , ‘fastway’ , ‘YUP’ );
$ar2 = array_count_values ( $ar ); // Normal matching
$ar = array_icount_values ( $ar ); // Case-insensitive matching
print_r ( $ar2 );
print_r ( $ar );
?>

Array
(
[J. Karjalainen] => 3
[60] => 2
[j. karjalainen] => 1
[Fastway] => 2
[FASTWAY] => 1
[fastway] => 1
[YUP] => 1
)
Array
(
[J. Karjalainen] => 4
[60] => 2
[Fastway] => 4
[YUP] => 1
)

I don’t know how efficient it is, but it seems to work. Needed this function in one of my scripts and thought I would share it.

A cleaner way to use array_count_values() to find boolean counts.

$list = [
[ ‘id’ => 1 , ‘result’ => true ],
[ ‘id’ => 2 , ‘result’ => true ],
[ ‘id’ => 3 , ‘result’ => false ],
];
$result = true ;

echo array_count_values ( array_map ( ‘intval’ , array_column ( $list , ‘result’ )))[(int) $result ];
// outputs: 2
?>

The case-insensitive version:

function array_count_values_ci ( $array ) $newArray = array();
foreach ( $array as $values ) if (! array_key_exists ( strtolower ( $values ), $newArray )) $newArray [ strtolower ( $values )] = 0 ;
>
$newArray [ strtolower ( $values )] += 1 ;
>
return $newArray ;
>
?>

Based on sergolucky96 suggestion
Simple way to find number of items with specific *boolean* values in multidimensional array:

$list = [
[ ‘id’ => 1 , ‘result’ => true ],
[ ‘id’ => 2 , ‘result’ => true ],
[ ‘id’ => 3 , ‘result’ => false ],
];
$result = true ;

echo array_count_values ( array_map (function( $v ) , array_column ( $list , ‘result’ )))[ $result ]
// outputs: 2

array_count_values function does not work on multidimentional arrays.
If $score[][] is a bidimentional array, the command
«array_count_values ($score)» return the error message «Warning: Can only count STRING and INTEGER values!».

  • Array Functions
    • array_​change_​key_​case
    • array_​chunk
    • array_​column
    • array_​combine
    • array_​count_​values
    • array_​diff_​assoc
    • array_​diff_​key
    • array_​diff_​uassoc
    • array_​diff_​ukey
    • array_​diff
    • array_​fill_​keys
    • array_​fill
    • array_​filter
    • array_​flip
    • array_​intersect_​assoc
    • array_​intersect_​key
    • array_​intersect_​uassoc
    • array_​intersect_​ukey
    • array_​intersect
    • array_​is_​list
    • array_​key_​exists
    • array_​key_​first
    • array_​key_​last
    • array_​keys
    • array_​map
    • array_​merge_​recursive
    • array_​merge
    • array_​multisort
    • array_​pad
    • array_​pop
    • array_​product
    • array_​push
    • array_​rand
    • array_​reduce
    • array_​replace_​recursive
    • array_​replace
    • array_​reverse
    • array_​search
    • array_​shift
    • array_​slice
    • array_​splice
    • array_​sum
    • array_​udiff_​assoc
    • array_​udiff_​uassoc
    • array_​udiff
    • array_​uintersect_​assoc
    • array_​uintersect_​uassoc
    • array_​uintersect
    • array_​unique
    • array_​unshift
    • array_​values
    • array_​walk_​recursive
    • array_​walk
    • array
    • arsort
    • asort
    • compact
    • count
    • current
    • end
    • extract
    • in_​array
    • key_​exists
    • key
    • krsort
    • ksort
    • list
    • natcasesort
    • natsort
    • next
    • pos
    • prev
    • range
    • reset
    • rsort
    • shuffle
    • sizeof
    • sort
    • uasort
    • uksort
    • usort
    • each

    Источник

Оцените статью