Home Plugin documentation Hooks and functions Functions pip_array_count_values_assoc()

 

 

Here we go!

 

pip_array_count_values_assoc()

Heads up!

This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don’t provide support for code customizations or 3rd party development.

Multi-dimensional version of PHP array_count_values() native function

/**
 * Multi-dimension version of array_count_values
 *
 * @see array_count_values()
 *
 * @param $index
 * @param $array
 *
 * @return array
 */
function pip_array_count_values_assoc( $array, $index ) {

    $result = array();

    foreach ( $array as $key => $value ) {

        // Check if value is already in result array
        if ( array_key_exists( $value[ $index ], $result ) ) {

            // Increment counter
            $result[ $value[ $index ] ] ++;

        } else {

            // New entry
            $result[ $value[ $index ] ] = 1;

        }
    }

    return $result;
}

You didn’t find the answer you were looking for?

or you think a specific topic deserve more informations?

 

Wave to bottom