[英]How to check if part of key is in a multi dimensional array
I am looking for a piece of code that can check whether a part of a "word" is inside of a multi-dimensional array. 我正在寻找一段代码,可以检查“单词”的一部分是否在多维数组内。
foreach($teamMembers as $key => $value){
if(preg_match('/^1.1./', $key)){
$countCount += $key;
$answerCount++;
}
}
I've tried to use this, but no luck. 我尝试使用此功能,但没有运气。
I need to count all numbers when the key
starts with '1.1.' 当
key
以“ 1.1”开头时,我需要计算所有数字。 It's inside a multidimensional array. 它在多维数组内。 Maybe that's the reason why it's not working?
也许这就是为什么它不起作用的原因?
The places where I need to get the data from is always different. 我需要从中获取数据的地方总是不同的。
$teamMembers[0]
$teamMembers[1]
etc. $teamMembers[0]
$teamMembers[1]
等。
Edit: 编辑:
The 1.1.
1.1.
does not contain any wildcards. 不包含任何通配符。 An array looks like this:
数组如下所示:
[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
)
)
EDIT: 编辑:
The next lines of code almost completely works. 接下来的几行代码几乎可以正常工作。
$count=0;
foreach($teamMembers[0] as $item){
if(is_array($item)){
foreach($item as $k=>$v){
if(strpos($k,'1.1')===0){
$count += $v;
}
}
}
}
But I need $teamMembers[0]
to go into the direction of $ teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
但是我需要
$teamMembers[0]
进入$ teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
behind it gives all 0
as output. teamMembers[0]['antwoord']. Else it'll pick up wrong data. Does anyone know how I can get this implemented? Just putting '['antwoord']
得到所有0
。
You can iterate each subarray and call array_filter()
with strpos()
then add that count to your total tally. 您可以迭代每个子
array_filter()
并使用strpos()
调用array_filter()
,然后将该计数添加到总计数中。
$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;
Output: 输出:
6
This is the same type of method with loops: 这与循环的方法类型相同:
$count=0;
foreach($teamMembers as $item){
if(is_array($item)){
foreach($item as $k=>$v){
if(strpos($k,'1.1.')===0){
++$count;
}
}
}
}
echo $count;
Furthermore... 此外...
If you want to target a specific subarray and count how many of those associative elements have a key that starts with a certain substring you can use: 如果要针对特定的子数组并计算有多少个关联元素的键以某个子字符串开头,则可以使用:
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
Or without a custom function call: 或没有自定义函数调用:
$answer_count=sizeof(array_filter($teamMembers['antwoord'],function($k){return strpos($k,'1.1.')===0;},ARRAY_FILTER_USE_KEY));
echo $answer_count;
// output: 3
array_walk_recursive
+ strpos
solution: 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.