Get count of each unique value in one column of a 2d array

Use array_key_exists & increment the count,

$newArray = array();
foreach ($the_outer_array as $key=>$value) {
    $firstValue = $value[0];
    if ($foundKey = array_key_exists($firstValue,$newArray)) {
        $newArray[$firstValue] += 1;
    }
   else{
        $newArray[$firstValue] = 1;
   }
}

DEMO.

Leave a Comment