簡體   English   中英

如何檢查鍵的一部分是否在多維數組中

[英]How to check if part of key is in a multi dimensional array

我正在尋找一段代碼,可以檢查“單詞”的一部分是否在多維數組內。

     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'] 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']得到所有0

您可以迭代每個子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;

輸出:

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

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM