不确定我是否在问正确的问题,但我有一个带有几个“List”名称的数组,我想计算它们在阵列中出现的次数并打印出来。这是清单:
$array = ['EMC_IMP_PAT_ANT_REVIEW_MANG_20160105', 'EMC_IMP_PAT_ANT_REVIEW_MANG_20160105', 'EMC_IMP_PAT_ANT_REVIEW_MANG_20150105', 'EMC_IMP_PAT_ANT_REVIEW_MANG_30160105', 'EMC_IMP_PAT_ANT_REVIEW_MANG_20160205', 'EMC_IMP_PAT_ANT_REVIEW_MANG_20178105'];
我目前正在使用print_r和array_count_values来打印和计算数组。
print_r(array_count_values($array));
我的输出是:
Array
(
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20160105] => 2
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20150105] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_30160105] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20160205] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20178105] => 1
)
我想以某种方式摆脱“数组”这个词和括号。我理想的输出是:
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20160105] => 2
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20150105] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_30160105] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20160205] => 1
[IMC_UMP_WRAP_ANNUAL_REVIEW_MANM_20178105] => 1
**方括号可以是可选的。谢谢!
答案 0 :(得分:2)
如果您不想迭代数组(如上面的评论中所述),请尝试使用preg_replace
删除不需要的部分:
$array = array(
'EMC_IMP_PAT_ANT_REVIEW_MANG_20160105',
'EMC_IMP_PAT_ANT_REVIEW_MANG_20160105',
'EMC_IMP_PAT_ANT_REVIEW_MANG_20150105',
'EMC_IMP_PAT_ANT_REVIEW_MANG_30160105',
'EMC_IMP_PAT_ANT_REVIEW_MANG_20160205',
'EMC_IMP_PAT_ANT_REVIEW_MANG_20178105',
);
echo preg_replace(
'/(^Array|^\\(\n|^\\)\n|^\s*)/m',
'',
print_r(array_count_values($array), true)
);
答案 1 :(得分:0)
使用Foreach
$data=array_count_values($array);
echo '<pre>';
foreach($data as $key => $value){
echo '[' . $key . '] =>' . $value . PHP_EOL;
}
echo '</pre>';