php count + array_filter多维数组中的多个值

时间:2014-02-14 14:52:20

标签: php arrays multidimensional-array 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 example:

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"
  }
}

3 个答案:

答案 0 :(得分: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
)

答案 1 :(得分:2)

在内存方面不是最有效的算法,但您可以将每个选项映射到新数组,然后使用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'));

答案 2 :(得分: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']]++;
  }
}