我有一个数组:
[873] => Array
(
[id] => 873
[email] =>
[data] => Array
(
[#1] => Dmitri Vegas & Like Mike
[#2] => Hardwell
[#3] => w&w
[#4] => alesso
[#5] => tiesto
[#6] => nicky romero
[#7] => dannic
[#8] => dj alvaro
[#9] => dvbbs
[#10] => sandro silva
)
)
[872] => Array
(
[id] => 872
[email] =>
[data] => Array
(
[#1] => nervo
[#2] => axwell
[#3] => armin van buuren
[#4] => tjr
[#5] => w&w
[#6] => deadmau5
[#7] => markus shulz
[#8] => chuckie
[#9] => alesso
[#10] => dada life
)
)
[871] => Array
(
[id] => 871
[email] =>
[data] => Array
(
[#1] => Noise controllers
[#2] => Noise controllers
[#3] => Noise crontrollers
[#4] => Zatox
[#5] => Zatox
[#6] => Zatox
[#7] => Zatox
[#8] => Hardwell
[#9] => Noisia
[#10] => Noisia
)
)
基本上我想做的是以标签格式跟踪关键字,有点像这样
Hardwell -> 2
w&w -> 2
tjr -> 1
etc etc
我知道我必须使用in_array()
搜索数组以查看该关键字出现的次数,但我很难在哪里开始
我很乐意将它们输出到一个列表中,根据哪个值具有最高的数字,我有点难过。任何人都可以告诉我如何做到这一点?
答案 0 :(得分:4)
这可以使用array_count_values()
:
$keywords = array();
foreach ($data as $key => $sub) {
foreach ($sub['data'] as $key => $value) {
$keywords[] = $value;
}
}
$result = array_count_values($keywords);
print_r($result);
$result
将是一个包含所有值和出现次数的数组。如果您还想显示出现的次数,您只需循环遍历$result
数组:
foreach ($result as $value => $number) {
echo "$value was found $number times".PHP_EOL;
}
答案 1 :(得分:0)
$array = array(
array('data' => array('asd','abc')),
array('data' => array('abc','cde')),
array('data' => array('cde','dfe'))
);
$data_array = array();
foreach ($array as $arr) {
if (is_array($arr['data']))
$data_array = array_merge($data_array, $arr['data']);
else
array_push($data_array, $arr['data']);
}
$new_arr = array_count_values($data_array);
arsort($new_arr);
print_r($new_arr);