计算数组数组中的匹配项 PHP
Count matches in array of arrays PHP
Array
(
[0] => Array
(
[song] => More Than A Feeling
[artist] => Not Boston
[time] => 15:00
)
[1] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[time] => 11:20
)
[2] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[time] => 15:23
)
)
有一个像这样的数组数组。我正在尝试计算所有匹配项。现在我正在使用
array_count_values(array_column($arr, 'song'));
这很好,但如果艺术家不匹配,它也会算作歌曲。我正在尝试输出以下内容。
Array
(
[0] => Array
(
[song] => More Than A Feeling
[artist] => Not Boston
[count] => 1
)
[1] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[count] => 2
)
)
不确定从哪里开始。感谢您的帮助!
在一个简单的循环中手动完成。我将搜索 $songs
数组并将元素添加到 $songCounters
中,不重复。输出 $songCounters
数组将包含歌曲和计数,其顺序是计数作为歌曲的下一个元素。
[(song)(count)(song)(count)]
代码如下:
//Here is your input array
$songs = array(0 => array('song' => 'More Than A Feeling', 'artist' => 'Not Boston', 'time' => 0),
1 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0),
2 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0));
$songCounters = array(); //Initialize the output array
//Now lets go through the input array
foreach($songs as $song) {
//Prepare the current song
$currentSong = array('song' => $song['song'], 'artist' => $song['artist']);
//Get the index of the current song from $songCounters
$index = array_search($currentSong, $songCounters);
//Insert if not found
if ($index == false) {
array_push($songCounters, $currentSong);
array_push($songCounters, 1); //Next element is the counter
}
else {
$songCounters[$index + 1]++; //Increase the counter if found
}
}
print_r($songCounters);
这里是phpfiddle.
找到了另一个问题的答案。这对我有用。
$arr = array(0 => array('song' => 'More Than A Feeling', 'artist' => 'Not Boston', 'time' => 0),
1 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0),
2 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0));
$hash = array();
$array_out = array();
foreach($arr as $item) {
$hash_key = $item['song'].'|'.$item['artist'];
if(!array_key_exists($hash_key, $hash)) {
$hash[$hash_key] = sizeof($array_out);
array_push($array_out, array(
'song' => $item['song'],
'artist' => $item['artist'],
'count' => 0,
));
}
$array_out[$hash[$hash_key]]['count'] += 1;
}
var_dump($array_out);
Array
(
[0] => Array
(
[song] => More Than A Feeling
[artist] => Not Boston
[time] => 15:00
)
[1] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[time] => 11:20
)
[2] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[time] => 15:23
)
)
有一个像这样的数组数组。我正在尝试计算所有匹配项。现在我正在使用
array_count_values(array_column($arr, 'song'));
这很好,但如果艺术家不匹配,它也会算作歌曲。我正在尝试输出以下内容。
Array
(
[0] => Array
(
[song] => More Than A Feeling
[artist] => Not Boston
[count] => 1
)
[1] => Array
(
[song] => More Than A Feeling
[artist] => Boston
[count] => 2
)
)
不确定从哪里开始。感谢您的帮助!
在一个简单的循环中手动完成。我将搜索 $songs
数组并将元素添加到 $songCounters
中,不重复。输出 $songCounters
数组将包含歌曲和计数,其顺序是计数作为歌曲的下一个元素。
[(song)(count)(song)(count)]
代码如下:
//Here is your input array
$songs = array(0 => array('song' => 'More Than A Feeling', 'artist' => 'Not Boston', 'time' => 0),
1 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0),
2 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0));
$songCounters = array(); //Initialize the output array
//Now lets go through the input array
foreach($songs as $song) {
//Prepare the current song
$currentSong = array('song' => $song['song'], 'artist' => $song['artist']);
//Get the index of the current song from $songCounters
$index = array_search($currentSong, $songCounters);
//Insert if not found
if ($index == false) {
array_push($songCounters, $currentSong);
array_push($songCounters, 1); //Next element is the counter
}
else {
$songCounters[$index + 1]++; //Increase the counter if found
}
}
print_r($songCounters);
这里是phpfiddle.
找到了另一个问题的答案。这对我有用。
$arr = array(0 => array('song' => 'More Than A Feeling', 'artist' => 'Not Boston', 'time' => 0),
1 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0),
2 => array('song' => 'More Than A Feeling', 'artist' => 'Boston', 'time' => 0));
$hash = array();
$array_out = array();
foreach($arr as $item) {
$hash_key = $item['song'].'|'.$item['artist'];
if(!array_key_exists($hash_key, $hash)) {
$hash[$hash_key] = sizeof($array_out);
array_push($array_out, array(
'song' => $item['song'],
'artist' => $item['artist'],
'count' => 0,
));
}
$array_out[$hash[$hash_key]]['count'] += 1;
}
var_dump($array_out);