1

I am wondering if I could explain this.

I have a multidimensional array , I would like to get the count of particular value appearing in that array

Below I am showing the snippet of array . I am just checking with the profile_type .

So I am trying to display the count of profile_type in the array

EDIT

Sorry I've forgot mention something, not something its the main thing , I need the count of profile_type==p

Array
(
    [0] => Array
        (
            [Driver] => Array
                (
                    [id] => 4
                    [profile_type] => p                    
                    [birthyear] => 1978
                    [is_elite] => 0
                )
        )
        [1] => Array
        (
            [Driver] => Array
                (
                    [id] => 4
                    [profile_type] => d                    
                    [birthyear] => 1972
                    [is_elite] => 1
                )
        )

)
0

5 Answers 5

2

Easy solution with RecursiveArrayIterator, so you don't have to care about the dimensions:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

$counter = 0
foreach ($iterator as $key => $value) {
  if ($key == 'profile_type' && $value == 'p') {
    $counter++;
  }
}
echo $counter;
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I've edited the question I need count of the profile_type==p
0

Something like this might work...

$counts = array();
foreach ($array as $key=>$val) {
    foreach ($innerArray as $driver=>$arr) {
        $counts[] = $arr['profile_type']; 
    }
}

$solution = array_count_values($counts);

1 Comment

Sorry I've edited the question I need count of the profile_type==p
0

I'd do something like:

$profile = array();
foreach($array as $elem) {
    if (isset($elem['Driver']['profile_type'])) {
        $profile[$elem['Driver']['profile_type']]++;
    } else {
        $profile[$elem['Driver']['profile_type']] = 1;
    }
}
print_r($profile);

Comments

0

You may also use array_walk($array,"test") and define a function "test" that checks each item of the array for 'type' and calls recursively array_walk($arrayElement,"test") for items of type 'array' , else checks for the condition. If condition satisfies, increment a count.

Comments

0

Hi You can get count of profuke_type==p from a multi dimensiona array

    $arr = array();
    $arr[0]['Driver']['id'] = 4;
    $arr[0]['Driver']['profile_type'] = 'p';
    $arr[0]['Driver']['birthyear'] = 1978;
    $arr[0]['Driver']['is_elite'] = 0;


    $arr[1]['Driver']['id'] = 4;
    $arr[1]['Driver']['profile_type'] = 'd';
    $arr[1]['Driver']['birthyear'] = 1972;
    $arr[1]['Driver']['is_elite'] = 1;

    $arr[2]['profile_type'] = 'p';
    $result = 0;
    get_count($arr, 'profile_type', 'd' , $result);
    echo $result;
    function get_count($array, $key, $value , &$result){
        if(!is_array($array)){
            return;
        }

        if($array[$key] == $value){
            $result++;
        }

        foreach($array AS $arr){
            get_count($arr, $key, $value , $result);
        }
    }

try this..

thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.