简体   繁体   中英

Count unique values in a column of an array

I have an array like this:

$arr = [
    1 => ['A' => '1', 'C' => 'TEMU3076746'],
    2 => ['A' => '2', 'C' => 'FCIU5412720'],
    3 => ['A' => '3', 'C' => 'TEMU3076746'],
    4 => ['A' => '4', 'C' => 'TEMU3076746'],
    5 => ['A' => '5', 'C' => 'FCIU5412720']
];

My goal is to count the distinct values in the C column of the 2-dimensional array.

The total rows in the array is found like this: count($arr) (which is 5 ).

How can I count the number of rows which contain a unique value in the 'C' column?

If I removed the duplicate values in the C column, there would only be: TEMU3076746 and FCIU5412720

My desired output is therefore 2 .

Hope this simplest one will be helpful. Here we are using array_column , array_unique and count .

Try this code snippet here

echo count(
        array_unique(
                array_column($data,"C")));

Result: 2

combine array_map , array_unique , count

$array = [ /* your array */ ];
$count = count(
    array_unique(
        array_map(function($element) { 
            return $element['C']; 
        }, $array))))

or use array_column as suggested by sahil gulati, array_map can do more stuff which probably isn't needed here.

I had a very similar need and I used a slightly different method.

I have several events where teams are participating and I need to know how many teams there are in each event. In other words, I don't need to only know how many distinct item "C" there are, but how many items TEMU3076746 and FCIU5412720 there are.

The code will then be as is

$nbCs = array_count_values ( array_column ( $array, 'C' ) );

$nbCs will issue an array of values = Array([TEMU3076746] => 3 [FCIU5412720] => 2)

See example in sandbox Sandbox code

Most concisely, use array_column() 's special ability to assign new first level keys using the targeted column's values. This provides the desired effect of uniqueness because arrays cannot contain duplicate keys on the same level.

Code: ( Demo )

$arr = [
    1 => ['A' => '1', 'C' => 'TEMU3076746'],
    2 => ['A' => '2', 'C' => 'FCIU5412720'],
    3 => ['A' => '3', 'C' => 'TEMU3076746'],
    4 => ['A' => '4', 'C' => 'TEMU3076746'],
    5 => ['A' => '5', 'C' => 'FCIU5412720']
];

echo count(array_column($arr, 'C', 'C'));
// 2

To be perfectly clear, array_column($arr, 'C', 'C') produces:

array (
  'TEMU3076746' => 'TEMU3076746',
  'FCIU5412720' => 'FCIU5412720',
)

This would also work with array_column($arr, null, 'C') , but that create a larger temporary array.


ps There is a fringe case that may concern researchers who are seeking unique float values. Assigning new associative keys using float values is inappropriate/error-prone because the keys will lose precision (become truncated to integers).

In that fringe case with floats, fallback to the less performant technique: count(array_unique(array_column($arr, 'B))) Demo

$data=array();
$data=[
       1 => [
             'A' => '1'
             'C' => 'TEMU3076746'
            ]

       2 => [
             'A' => '2'
             'C' => 'FCIU5412720'
            ]

       3 => [
             'A' => '3'
             'C' => 'TEMU3076746'
            ]
       
       4 => [
            'A' => '4'
            'C' => 'TEMU3076746'
            ]

       5 => [
             'A' => '5'
             'C' => 'FCIU5412720'
           ]
        ];

 $total_value=count(
    array_unique(
            array_column($data,"C")));
echo $total_value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM