我正在寻找一段代码,可以检查一个"字的一部分"在多维数组中。
foreach($teamMembers as $key => $value){
if(preg_match('/^1.1./', $key)){
$countCount += $key;
$answerCount++;
}
}
我试过用这个,但没有运气。
我需要在key
以' 1.1开头时计算所有数字。'
它位于多维数组中。也许这就是它无法正常工作的原因?
我需要从中获取数据的地方总是不同的。
$teamMembers[0]
$teamMembers[1]
等。
编辑:
1.1.
不包含任何通配符。数组如下所示:
[1] => Array
(
[respondentNummer] => Respondent 2
[respondentNaam] => Dred Back
[respondentGroep] => Team members
[respondentScoRev] => SCORE
[antwoord] => Array
(
[1.1. Common Values and Purpose, The purpose of the team is well known] => 4
[1.1. Common Values and Purpose, The overriding objectives of the team are known] => 4
[1.1. Common Values and Purpose, The direction in which the team should work is clear] => 4
[1.2. Clear Goals, The goals the team must reach are clearly stated.] => 4
[1.2. Clear Goals, The members share their views of the goals of the team] => 4
[1.2. Clear Goals, The results expected of the team are clear to the members] => 4
[1.3. Aligned Direction, The team tackles problems in a way that is determined in advance] => 4
[1.3. Aligned Direction, All members of the team agree on the way the work is conducted] => 4
[1.3. Aligned Direction, Everyone in the team has the same opinion as to how the team should work] => 4
[1.4. Defined authority, The team knows the extent of its authority] => 4
[1.4. Defined authority, The team's sphere of responsibility is precisely delimited] => 4
[1.4. Defined authority, It is clear to everyone what the team's authority of decision is] => 4
[2.1. Commitment to team, Members back up the decisions made, even if they do not fully agree] => 4
[2.1. Commitment to team, The team feels a common responsibility for the results gained] => 4
[2.1. Commitment to team, The team assumes a common accountability for bad results] => 4
[2.2. Effective composition , The team utilises the strengths of the members] => 4
[2.2. Effective composition , Team members supplement each other] => 4
[2.2. Effective composition , The members all contribute their skills] => 4
[2.3. Team Role Behaviour, The team's efforts is beneficial to the rest of the organisation Everyone knows the purpose of the team] => 4
[2.3. Team Role Behaviour, The overriding objectives of the team are known] => 4
[2.3. Team Role Behaviour, The team knows what its main tasks are] => 4
[2.4. Functional expertise, The team has insight into specialist areas] => 4
[2.4. Functional expertise, The team has knowledge of all important areas] => 4
[2.4. Functional expertise, All important functional areas are represented in the team] => 4
[3.1. Team rules followed, All mutual appointments are held] => 4
[3.1. Team rules followed, Everyone arrives to meetings in time] => 4
[3.1. Team rules followed, Team members are prepared for the meetings] => 4
[3.2. Effective Decision making, The team usually agrees on decisions made] => 4
[3.2. Effective Decision making, The team seeks to reach agreement on major decisions] => 4
[3.2. Effective Decision making, Important decisions are made in consent] => 4
[3.3. Flexible and responsive, The team reacts to changes in the business environment] => 4
[3.3. Flexible and responsive, The team reconsiders issues when the conditions are changed] => 4
[3.3. Flexible and responsive, The team pays attention to changes that occur] => 4
[3.4. Good relationships, Everybody respects the others' opinions] => 4
[3.4. Good relationships, The members show consideration for the dissimilarity of the others] => 4
[3.4. Good relationships, The members respect that opinions may differ] => 4
[4.1. Business and result focus, The team's efforts lead to tangible results] => 4
[4.1. Business and result focus, The team strives to show visible results] => 4
[4.1. Business and result focus, The team sets result objectives for all important functional areas] => 4
[4.2. Improvement orientation, The team takes new initiatives in time] => 4
[4.2. Improvement orientation, The team will continuously ensure that the organisation can lift its tasks] => 4
[4.2. Improvement orientation, The team will allocate extra resources as needed] => 4
[4.3. Positive relations outside of team, The members of the team listen to the staff] => 4
[4.3. Positive relations outside of team, The team seeks information in the organization] => 4
[4.3. Positive relations outside of team, The team makes clear information available on its decisions] => 4
[4.4. Sets business direction, The team knows which issues to deal with] => 4
[4.4. Sets business direction, The team emphasises the implementing of decisions made] => 4
[4.4. Sets business direction, The team's decisions are implemented] => 4
[4.5. Continual improvement, The team thinks about how to take decisions and how to improve them for the future] => 4
)
[weging] => Array
(
[1.1. Common Values and Purpose, The purpose of the team is well known] => 4
[1.1. Common Values and Purpose, The overriding objectives of the team are known] => 4
[1.1. Common Values and Purpose, The direction in which the team should work is clear] => 4
[1.2. Clear Goals, The goals the team must reach are clearly stated.] => 4
[1.2. Clear Goals, The members share their views of the goals of the team] => 4
[1.2. Clear Goals, The results expected of the team are clear to the members] => 4
[1.3. Aligned Direction, The team tackles problems in a way that is determined in advance] => 4
[1.3. Aligned Direction, All members of the team agree on the way the work is conducted] => 4
[1.3. Aligned Direction, Everyone in the team has the same opinion as to how the team should work] => 4
[1.4. Defined authority, The team knows the extent of its authority] => 4
[1.4. Defined authority, The team's sphere of responsibility is precisely delimited] => 4
[1.4. Defined authority, It is clear to everyone what the team's authority of decision is] => 4
[2.1. Commitment to team, Members back up the decisions made, even if they do not fully agree] => 4
[2.1. Commitment to team, The team feels a common responsibility for the results gained] => 4
[2.1. Commitment to team, The team assumes a common accountability for bad results] => 4
[2.2. Effective composition , The team utilises the strengths of the members] => 4
[2.2. Effective composition , Team members supplement each other] => 4
[2.2. Effective composition , The members all contribute their skills] => 4
[2.3. Team Role Behaviour, The team's efforts is beneficial to the rest of the organisation Everyone knows the purpose of the team] => 4
[2.3. Team Role Behaviour, The overriding objectives of the team are known] => 4
[2.3. Team Role Behaviour, The team knows what its main tasks are] => 4
[2.4. Functional expertise, The team has insight into specialist areas] => 4
[2.4. Functional expertise, The team has knowledge of all important areas] => 4
[2.4. Functional expertise, All important functional areas are represented in the team] => 4
[3.1. Team rules followed, All mutual appointments are held] => 4
[3.1. Team rules followed, Everyone arrives to meetings in time] => 4
[3.1. Team rules followed, Team members are prepared for the meetings] => 4
[3.2. Effective Decision making, The team usually agrees on decisions made] => 4
[3.2. Effective Decision making, The team seeks to reach agreement on major decisions] => 4
[3.2. Effective Decision making, Important decisions are made in consent] => 4
[3.3. Flexible and responsive, The team reacts to changes in the business environment] => 4
[3.3. Flexible and responsive, The team reconsiders issues when the conditions are changed] => 4
[3.3. Flexible and responsive, The team pays attention to changes that occur] => 4
[3.4. Good relationships, Everybody respects the others' opinions] => 4
[3.4. Good relationships, The members show consideration for the dissimilarity of the others] => 4
[3.4. Good relationships, The members respect that opinions may differ] => 4
[4.1. Business and result focus, The team's efforts lead to tangible results] => 4
[4.1. Business and result focus, The team strives to show visible results] => 4
[4.1. Business and result focus, The team sets result objectives for all important functional areas] => 4
[4.2. Improvement orientation, The team takes new initiatives in time] => 4
[4.2. Improvement orientation, The team will continuously ensure that the organisation can lift its tasks] => 4
[4.2. Improvement orientation, The team will allocate extra resources as needed] => 4
[4.3. Positive relations outside of team, The members of the team listen to the staff] => 4
[4.3. Positive relations outside of team, The team seeks information in the organization] => 4
[4.3. Positive relations outside of team, The team makes clear information available on its decisions] => 4
[4.4. Sets business direction, The team knows which issues to deal with] => 4
[4.4. Sets business direction, The team emphasises the implementing of decisions made] => 4
[4.4. Sets business direction, The team's decisions are implemented] => 4
[4.5. Continual improvement, The team thinks about how to take decisions and how to improve them for the future] => 4
)
)
编辑:
下一行代码几乎完全有效。
$count=0;
foreach($teamMembers[0] as $item){
if(is_array($item)){
foreach($item as $k=>$v){
if(strpos($k,'1.1')===0){
$count += $v;
}
}
}
}
但我需要$teamMembers[0]
进入$ teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
后面的方向,并将所有0
作为输出。
答案 0 :(得分:1)
您可以迭代每个子阵列并使用array_filter()
致电strpos()
,然后将该计数添加到总计数中。
代码:(Demo)
$count=0;
foreach($teamMembers as $item){
if(is_array($item)){
$count+=sizeof(array_filter($item,function($k){return strpos($k,'1.1.')===0;},ARRAY_FILTER_USE_KEY));
}
}
echo $count;
输出:
6
这是带循环的相同类型的方法:
$count=0;
foreach($teamMembers as $item){
if(is_array($item)){
foreach($item as $k=>$v){
if(strpos($k,'1.1.')===0){
++$count;
}
}
}
}
echo $count;
...此外
如果要定位特定子阵列并计算其中有多少个关联元素具有以某个子字符串开头的键,则可以使用:
function count_keys_beginning_with($array,$substring){
return sizeof(array_filter($array,function($k)use($substring){return strpos($k,$substring)===0;},ARRAY_FILTER_USE_KEY));
}
echo count_keys_beginning_with($teamMembers['antwoord'],'1.1.');
// output: 3
或者没有自定义函数调用:
$answer_count=sizeof(array_filter($teamMembers['antwoord'],function($k){return strpos($k,'1.1.')===0;},ARRAY_FILTER_USE_KEY));
echo $answer_count;
// output: 3
答案 1 :(得分:1)
array_walk_recursive
+ strpos
解决方案:
$teamMembers = [
'responderScoRev' => 'Score',
'antwoord' => ['1.2. text' => 1, '1.1.2 text' => 1, '1.1.3 text' => 1],
'1.1.3' => 3
];
$cnt = 0;
array_walk_recursive($teamMembers, function($v, $k) use(&$cnt){
if (strpos($k, '1.1.') === 0) $cnt++;
});
print_r($cnt);