如何递归解析数组,其中我计算具有特定值的键的数量

Here is the problem: I extract the values from a DB and they are placed in an array. The array contains multiple sports teams. Each team has multiple members. However the number of members varies with each team.

So, I need to parse through the array and starting with team 1 count how many members it has, placing the value in a variable. Once complete it goes to team 2, then team 3 and so forth. When all teams are counted then the count results of each team will be presented on a web page.

It is irrelevant to the problem, but I am using Laravel. However, at this stage that is not my primary concern. Understanding a solution in raw php is a good start.

Try using array_map:

$teams = array(
    'Team 1' => array('Joe', 'Jane'),
    'Team 2' => array('Foo', 'Bar'),
);

$counts = array_map(function($team) {
    return count($team);
}, $teams);

See example here: http://ideone.com/zfvIw2

When dealing with Eloquent collections in Laravel, the answer will be a little different.

So many options.

Best bet. Split the teams into different arrays. Then you can just look at the array lenght.

This would work using a multidimensional array as well...

Playerset [team] = array ( array ( name, age, sex, game), array( name, age, sex, game) ).

You can use below logic for this for example :- $data=array('team1'=>array('a','b','c'), 'team2'=>array('d','e'), 'team3'=>array('f'));

$count=0;foreach($data as $key=>$val){ $count[$key]=count($data[$key]);}

print_r($count) will give you like this

$count=array('team1'=>3,'team2=>2,'team'=>1);

Hope this will help you in solving your problem