MenCoder
MenCoder

Reputation: 11

How to know how many time the same array are present inside another big array in php matrix array

I try to make a counter of how many times all the arrays are present in the big array.

$array = [[121,159,196],[121,159,196],[121,159,196],[121,159,196],[120,158,195],[119,157,194],[118,156,193],[117,155,192],[119,157,194],[118,156,193],[119,157,194],[121,159,196],[122,160,197],[122,160,197],[123,161,198],[124,162,199],[121,159,196],[122,160,197],[122,160,197],[123,161,198],[123,161,198],[123,161,198],[122,160,197],[122,160,197],[121,159,196],[121,159,196],[121,159,196],[121,159,196],[120,158,195],[120,158,195],[120,158,195],[120,158,195],[117,157,193],[118,158,194],[119,159,195],[119,159,195],[119,159,195],[119,159,195],[120,160,196],[121,161,197],[118,158,194],[118,158,194],[118,158,194],[119,159,195],[120,160,196],[121,161,197],[121,161,197],[120,160,196],[118,158,194],[118,158,194],[118,158,194],[117,157,193],[117,157,193],[117,157,193],[116,156,192],[116,156,192],[117,157,193],[117,157,193],[116,156,192],[117,157,193],[117,157,193],[118,158,194],[120,160,196],[120,160,196],[121,159,196],[121,159,196],[120,158,195],[120,158,195],[120,158,195],[119,157,194],[119,157,194],[119,157,194],[121,159,196],[120,158,195],[119,157,194],[118,156,193],[118,156,193],[119,157,194],[120,158,195],[121,159,196],[121,157,193],[121,157,193],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[121,157,193],[121,157,193],[122,158,194],[122,158,194],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[121,157,193],[121,157,193],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[121,157,193],[122,158,194],[120,156,192],[120,156,192],[120,156,192],[119,155,191],[120,156,192],[119,155,191],[120,156,192],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[119,155,191],[123,159,195],[123,159,195]];
    
$count_array = [];
    
foreach($array as $item){
    
    if(!isset($count_array[$item])){
    
        $count_array[$item] =1 ;
    }
    
    else{
    
       $count_array[$item]++;
    }
}

Expected:

$count_array = [121,159,196] = times , [119,157,194] = times 

etc etc.

Upvotes: 0

Views: 63

Answers (3)

jspit
jspit

Reputation: 7693

A very smart solution uses array_count_values. Since the function cannot count arrays, the subarrays are converted to strings with json_encode beforehand.

$jArray = array_map('json_encode',$array);
$countArr = array_count_values($jArray);

Demo: https://3v4l.org/fqWeR

Upvotes: 2

C. Celora
C. Celora

Reputation: 429

You can't use an array as a key of another array, but you can implode the $item variable to create a string key with the items inside.

The code should be like this:

$count_array = [];
    
foreach($array as $item){
    $item_key = implode(",",$item);
    if(!isset($count_array[$item_key])){  
        $count_array[$item_key] = 1;
    }else{
       $count_array[$item_key]++;
    }
}

var_dump($count_array);
/**
 * array(19) {
 *   ["121,159,196"]=>
 *    int(14)
 *   ["120,158,195"]=>
 *    int(10)
 * 
 *     ...
 *  }
 */

With this solution you may have a problem with the order of the items in the matrix.

In this case if you have [121,159,196] will be considered different as [159,126,196]. If you need to count them as the same value you need to order the $item elements inside the matrix.

Upvotes: 0

ADyson
ADyson

Reputation: 61859

You didn't tell us what error you're having, but from running it I see you would get this messsage:

Fatal error: Uncaught TypeError: Illegal offset type in isset or empty

This is because can't use an array as a key for another array. Array keys must be strings or integers. So you'd have to turn your array into a string first. Encoding it as JSON is a convenient way to get the format you're looking for.

Here's some sample code. I also added a simple loop to output it the way you mentioned:

$array = [[121,159,196],[121,159,196],[121,159,196],[121,159,196],[120,158,195],[119,157,194],[118,156,193],[117,155,192],[119,157,194],[118,156,193],[119,157,194],[121,159,196],[122,160,197],[122,160,197],[123,161,198],[124,162,199],[121,159,196],[122,160,197],[122,160,197],[123,161,198],[123,161,198],[123,161,198],[122,160,197],[122,160,197],[121,159,196],[121,159,196],[121,159,196],[121,159,196],[120,158,195],[120,158,195],[120,158,195],[120,158,195],[117,157,193],[118,158,194],[119,159,195],[119,159,195],[119,159,195],[119,159,195],[120,160,196],[121,161,197],[118,158,194],[118,158,194],[118,158,194],[119,159,195],[120,160,196],[121,161,197],[121,161,197],[120,160,196],[118,158,194],[118,158,194],[118,158,194],[117,157,193],[117,157,193],[117,157,193],[116,156,192],[116,156,192],[117,157,193],[117,157,193],[116,156,192],[117,157,193],[117,157,193],[118,158,194],[120,160,196],[120,160,196],[121,159,196],[121,159,196],[120,158,195],[120,158,195],[120,158,195],[119,157,194],[119,157,194],[119,157,194],[121,159,196],[120,158,195],[119,157,194],[118,156,193],[118,156,193],[119,157,194],[120,158,195],[121,159,196],[121,157,193],[121,157,193],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[121,157,193],[121,157,193],[122,158,194],[122,158,194],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[121,157,193],[121,157,193],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[121,157,193],[122,158,194],[120,156,192],[120,156,192],[120,156,192],[119,155,191],[120,156,192],[119,155,191],[120,156,192],[121,157,193],[121,157,193],[120,156,192],[120,156,192],[120,156,192],[120,156,192],[119,155,191],[123,159,195],[123,159,195]];
    
$count_array = [];
    
foreach($array as $item){
    
    $key = json_encode($item);
    
    if(!isset($count_array[$key])){
    
        $count_array[$key] =1 ;
    }
    
    else{
    
       $count_array[$key]++;
    }
}

foreach ($count_array as $key => $val)
{
    echo $key.": ".$val." times".PHP_EOL;
}

Live demo: https://3v4l.org/gRPe1

Upvotes: 1

Related Questions