PHP count values in multidimensional-array where key and value equals

rwzdoorn

I'm trying to create a dashboard with some information like how many users are male or female. I'm storing profile information already in one multidimensional-array called 'users'. To count the total male/female users I want to use a function with multiple arguments (the array, key and value.

I tried the following code:

  <?
    function countArray($array, $key, $value) {
      $cnt = count(array_filter($array,function($element) {
        return $element[$key]== $value;
      }));
      echo $cnt;
    }

    countArray($users, 'gender', '1');
  ?>

This results in Undefined variable: key/values. What am I doing wrong?

B. Fleming

The problem is that anonymous functions in PHP do not have access to variables outside of their own scope. As such, the array_filter() callback function you've provided has no knowledge of $key or $value, and so they're undefined variables within that function scope. To fix this, you must explicitly pass the external variables into the function scope using the use keyword, like function() use ($external_variable) {}.

In your case, the solution would look something like this:

<?
    function countArray($array, $key, $value) {
      $cnt = count(array_filter($array,function($element) use ($key, $value) {
        return $element[$key]== $value;
      }));
      echo $cnt;
    }

    countArray($users, 'gender', '1');
?>

If you're using PHP 7.4 or above, you can also just use an arrow function to allow the external scope to become part of the function scope implicitly:

<?
    function countArray($array, $key, $value) {
      $cnt = count(array_filter($array,fn($element) => $element[$key]== $value ));
      echo $cnt;
    }

    countArray($users, 'gender', '1');
?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP Multidimensional Array search where key equals and return main key

PHP - update multidimensional array value where key

Getting the values of a multidimensional array where key value is in another array

PHP: Addition of values in multidimensional array where key equal to and equal to

PHP count Multidimensional Array value

PHP explode string key into multidimensional array with values

Group multidimensional array by key and sum values in PHP

Compare multidimensional array values by key in php

php count array multidimensional with loop by value

PHP multidimensional array: retrieve the value given a key

How to search by key=>value in a multidimensional array in PHP

Check if a key value exist into a multidimensional array with PHP

Multidimensional Search in Array By Key Value [PHP]

Return value from multidimensional array by key in PHP

Check if a key has a value in a multidimensional array with php

php multidimensional array merge with same key and value

Multidimensional array "join" in PHP based on a key value

How to get values in one column where another column equals a value with SQL, and store into an array in PHP?

php multidimensional array count duplicate values and add corresponding values

php multidimensional array unique with different value make key value array

Retrieve javascript array object data where key equals a value

How to remove/update a JSONB array element where key equals a value?

Merge array depending key values in a multidimensional array in PHP

php: How to add generically values to a multidimensional array without incrementing the count

PHP - Count number of values in a specific index of multidimensional array

Handle PHP multidimensional array to assign key as parent of value array

Get lowest key - array value in multidimensional array PHP

Extract values from multidimensional array using key value

How to delete elements from multidimensional array where sum of values equals 0?