0
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
    )
)

Have an array of arrays like this. I am trying to count all matches. Right now i'm using

array_count_values(array_column($arr, 'song'));

That's good but it counts songs if the artist doesn't match. I am trying to output the following.

Array
    (
    [0] => Array
    (
        [song] => More Than A Feeling
        [artist] => Not Boston
        [count] => 1
    )

    [1] => Array
    (
        [song] => More Than A Feeling
        [artist] => Boston
        [count] => 2
    )
)

Not sure where to start. Thanks for the help!

Bennett
  • 1
  • 2

2 Answers2

0

Do it manually in a simple loop. I'm going to search through the $songs array and add the elements to $songCounters with no duplicates. The output $songCounters array will contain both the songs and the count in such an order that the count comes as the next element to the song.

[(song)(count)(song)(count)]

Here is the code:

//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);

Here is the php fiddle.

Charlie
  • 22,886
  • 11
  • 59
  • 90
  • That sort of works. Gives me something to work off. I was trying to add the count into the original song array instead of yours it puts it in a seperate array. I won't be able to sort the array after. But thanks it might help me get further. – Bennett Jan 20 '16 at 21:10
  • Anyway you needed a separate array for output. Thats the reason this is done with another array. – Charlie Jan 21 '16 at 04:25
0

Found the answer on a different question. This worked for me.

$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);
Bennett
  • 1
  • 2