3

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..

1
  • There's no $request_params[2]. Just $request_params['chat_id'] and $request_params['text']. Commented Dec 18, 2017 at 21:47

2 Answers 2

2

You don't need [2]:

echo count($request_params['text']);
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

@slajostealos Andreas is reasonably confused here. Please improve your question to express your desired output. (for the sake of all future SO readers)

Your Answer

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