user9115161
user9115161

Reputation:

How to count number Array which inside of another Array

Lets say I have an Array like this:

print_r($request_params);

Array ( 
    [chat_id] => 120613381 
    [text] => Array ( 
        [0] => saadat 
        [1] => saadat 
        [2] => saadat 
        [3] => saadat 
        [4] => saadat 
        [5] => saadat 
        [6] => donya 
        [7] => donya 
        [8] => donya 
        [9] 
    )
)

As you can see inside of [text] I have multiple values. Now I want to count that but I don't know how to access that part.

My try was this but it returns 0:

echo $num3 = count($request_params[2]["text"]);

So what is your suggest..

Upvotes: 3

Views: 56

Answers (2)

Andreas
Andreas

Reputation: 23958

I think you are looking for array_count_values.
It will return an associative array with the count of each name.

$counts = array_count_values($request_params['text']);
Var_dump($counts); 

This should return for example:
[saadat] => 6
[donya] => 3

Upvotes: 0

Jeffwa
Jeffwa

Reputation: 1143

You don't need [2]:

echo count($request_params['text']);

Upvotes: 2

Related Questions