2014-02-14 52 views
3

如何防止爲每個要搜索的值重複相同的代碼塊?php count + array_filter多維數組中的多個值

我想通過計算另一個多維數組($ data)中的特定值來創建一個新數組($ result)。

$result = array(); 

    $result['Insulin'] = count(array_filter($data,function ($entry) { 
              return ($entry['choice'] == 'Insulin'); 
             } 
            ) 
           ); 

    $result['TZD'] = count(array_filter($data,function ($entry) { 
              return ($entry['choice'] == 'TZD'); 
             } 
            ) 
           ); 

    $result['SGLT-2'] = count(array_filter($data,function ($entry) { 
              return ($entry['choice'] == 'SGLT-2'); 
             } 
            ) 
           ); 

$ data數組例如:

array(2) { 
    [0]=> 
    array(9) { 
    ["breakout_id"]=> 
    string(1) "1" 
    ["case_id"]=> 
    string(1) "1" 
    ["stage_id"]=> 
    string(1) "1" 
    ["chart_id"]=> 
    string(1) "1" 
    ["user_id"]=> 
    string(2) "10" 
    ["region"]=> 
    string(6) "Sweden" 
    ["choice"]=> 
    string(7) "Insulin" 
    ["switched_choice"]=> 
    NULL 
    ["keep"]=> 
    string(1) "1" 
    } 
    [1]=> 
    array(9) { 
    ["breakout_id"]=> 
    string(1) "1" 
    ["case_id"]=> 
    string(1) "1" 
    ["stage_id"]=> 
    string(1) "1" 
    ["chart_id"]=> 
    string(1) "1" 
    ["user_id"]=> 
    string(1) "7" 
    ["region"]=> 
    string(6) "Sweden" 
    ["choice"]=> 
    string(7) "Insulin" 
    ["switched_choice"]=> 
    NULL 
    ["keep"]=> 
    string(1) "1" 
    } 
} 

回答

2

可以將您的匿名函數轉換成與use關鍵字封閉。傳入您想要匹配的值的字符串變量。

// Array of strings to match and use as output keys 
$keys = array('Insulin','TZD','SGLT-2'); 
// Output array 
$result = array(); 

// Loop over array of keys and call the count(array_filter()) 
// returning the result to $result[$key] 
foreach ($keys as $key) { 
    // Pass $key to the closure 
    $result[$key] = count(array_filter($data,function ($entry) use ($key) { 
    return ($entry['choice'] == $key); 
    })); 
} 

轉換您的var_dump()到一個數組並運行該反對的話,輸出是:

Array 
(
    [Insulin] => 2 
    [TZD] => 0 
    [SGLT-2] => 0 
) 

你可以用array_count_values()簡化它,以及:

$result2 = array_count_values(array_map(function($d) { 
    return $d['choice']; 
}, $data)); 

print_r($result2); 

Array 
(
    [Insulin] => 2 
) 

如果您需要零計數爲缺少的,你可以使用array_merge()

// Start with an array of zeroed values 
$desired = array('Insulin'=>0, 'TZD'=>0, 'SGLT-2'=>0); 
// Merge it with the results from above 
print_r(array_merge($desired, $result2)); 
Array 
(
    [Insulin] => 2 
    [TZD] => 0 
    [SGLT-2] => 0 
) 
0

您可以輕鬆簡化您的代碼,如果計數是你唯一的目標:

$result = array(); 

foreach ($data AS $row) { 
    if (!isset($result[$row['choice']])) { 
    $result[$row['choice']] = 1; 
    } else { 
    $result[$row['choice']]++; 
    } 
} 

如果你想只計算具體的選擇,可以改變上面的代碼弄成這個樣子:

$result = array(); 
$keys = array('Insulin', 'TZD', 'SGLT-2'); 

foreach ($data AS $row) { 
    if (!in_array($row['choice'], $keys)) { 
    continue; 
    } 

    if (!isset($result[$row['choice']])) { 
    $result[$row['choice']] = 1; 
    } else { 
    $result[$row['choice']]++; 
    } 
} 
+0

你可以做'$鍵= array_flip([ '胰島素', 'TZD', 'SGLT-2']);'和然後做'if(!isset($ keys [$ row ['choice'])){continue; ''以後。 –

1

不是內存方面最高效的算法,但是您可以將每個選項映射到新陣列上,然後使用array_count_values()

$result = array_count_values(array_map(function($item) { 
    return $item['choice']; 
}, $data)); 

由於5.5可以使用array_column()簡化多一點:

$result = array_count_values(array_column($data, 'choice'));