Viewed   118 times

I have a multidimensional array called $songs, which outputs the following:

Array
(
    [0] => Array
        (
            [Michael Jackson] => Thriller
        )

    [1] => Array
        (
            [Michael Jackson] => Rock With You
        )

    [2] => Array
        (
            [Teddy Pendergrass] => Love TKO
        )

    [3] => Array
        (
            [ACDC] => Back in Black
        )
)

I would like to merge the arrays which have duplicate keys, so I can get the following:

Array
(
    [0] => Array
        (
            [Michael Jackson] => Array
            (
                [0] => Thriller
                [1] => Rock With You
            )
        )

    [1] => Array
        (
            [Teddy Pendergrass] => Love TKO
        )

    [2] => Array
        (
            [ACDC] => Back in Black
        )
)

How do I do this?

Bonus points for giving me the code to output the array like:

<h2>Michael Jackson</h2>
<ul>
<li>Thriller</li>
<li>Thriller</li>
</ul>

<h2>Teddy Pendergrass</h2>
<ul>
<li>Love TKO</li>
</ul>

<h2>ACDC</h2>
<ul>
<li>Back in Black</li>
</ul>

 Answers

2

This should do it, it's not exactly what you want but I don't see a reason why you'd need to index the resulting array numerically, and then by artist.

$source = array(
    array('Michael Jackson' => 'Thriller'),
    array('Michael Jackson' => 'Rock With You'),
    array('Teddy Pendergrass' => 'Love TKO'),
    array( 'ACDC' => 'Back in Black')
);

$result = array();

foreach($source as $item) {
    $artist = key($item);
    $album = current($item);

    if(!isset($result[$artist])) {
        $result[$artist] = array();
    }
    $result[$artist][] = $album;
}

And you can loop the $result array and build your HTML like this:

foreach($result as $artist => $albums) {
    echo '<h2>'.$artist.'</h2>';
    echo '<ul>';
    foreach($albums as $album) {
        echo '<li>'.$album.'</li>';
    }
    echo '</ul>';
}

Which would result in a similar list that you described.

Monday, October 31, 2022
4

You need to iterate over your results, adding a new entry to the output when you encounter a new team, or updating the points value when you find the same team again. This is most easily done by initially indexing the output by the team name, and then using array_values to re-index the array numerically:

$teams = array();
foreach ($results as $result) {
    $team = $result['team'];
    if (!isset($teams[$team])) {
        $teams[$team] = array('team' => $team, 'points' => $result['punti']);
    }
    else {
        $teams[$team]['points'] += $result['punti'];
    }
}
$teams = array_values($teams);
print_r($teams);

Output (for your sample data):

Array
(
    [0] => Array
        (
            [team] => Red Bull Racing
            [points] => 418
        )
    [1] => Array
        (
            [team] => Scuderia Ferrari
            [points] => 353
        )
    [2] => Array
        (
            [team] => Mercedes-AMG
            [points] => 516
        )
    [3] => Array
        (
            [team] => Racing Point F1
            [points] => 147
        )
    [4] => Array
        (
            [team] => Haas F1
            [points] => 127
        )
)

Demo on 3v4l.org

Friday, August 12, 2022
 
1
function group_by_key ($array) {
  $result = array();
  foreach ($array as $sub) {
    foreach ($sub as $k => $v) {
      $result[$k][] = $v;
    }
  }
  return $result;
}

See it working

Monday, October 17, 2022
 
4

Can never avoid the loop :-)

function search($array, $searchString){
   foreach($array as $key=>$val){
        if(in_array($searchString, $val)) return true;
   }
   return false;
}

//use it like so:
if(search($array, '109.148.183.1')){/*do something*/}
Friday, September 16, 2022
 
3

You can do it like this, do the calculation from the innermost of the array. Check the demo.

<?php
function f(&$array)
{
    foreach($array as $k => &$v)
    {
        if(is_array($v))
        {
            if(count($v) == count($v, 1))
            {
                unset($array[$k]);
                if($k == 'sum')
                {
                    $v =  array_sum($v);
                    $array[] = $v;

                }elseif($k == 'multiply'){
                    $v = array_product($v);
                    $array[] = $v;
                }else{

                    foreach($v as $vv)
                        $array[] = $vv;
                }
            }else
                f($v);
        }
    }
}

while(count($array) != count($array, 1))
{
    f($array);
}

print_r($array);

Noteļ¼š

traverse array from outer to inner
traverse array from inner to outer

Monday, November 14, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :