Post count by meta_key meta_value [closed]

// retrieve all meta_values with key 'state' from database
$state_posts = $wpdb->get_results("
    SELECT meta_value FROM ".
    $wpdb->prefix."postmeta
    WHERE meta_key = 'state'
    ORDER BY meta_value ASC",
    ARRAY_A
);

// define counting array
$state_count = array();

// iterate through meta_values, count the occurence of each state
foreach ( $state_posts as $state_post ) {
    if ( isset ( $state_count[$state_post['meta_value']] ) ) {
        $state_count[$state_post['meta_value']] = $state_count[$state_post['meta_value']] + 1;
    } else {
        $state_count[$state_post['meta_value']] = 1;
    }
}

// echo results
echo '<ul>';
foreach ( $state_count as $state => $count ) {
     echo '<li>' . $state . ': ' . $count . ' posts</li>';
}
echo '</ul>';

tech