0

Array #1 looks something like this (simplified):

Array
(
    [Appetizers] => Array
        (
            [0] => Array
                (
                    [category] => Appetizers
                    [name] => Beef Tenderloin Carpaccio
                )

            [1] => Array
                (
                    [category] => Appetizers
                    [name] => Calamari Fritto Misto
                )    
        )

    [Flatbreads] => Array
        (
            [0] => Array
                (
                    [category] => Flatbreads
                    [name] => Quattro Formaggi
                )

            [1] => Array
                (
                    [category] => Flatbreads
                    [name] => Tomato & Burrata
                )

        )
)

Array #2 Looks like this:

Array
(
    [0] => Array
        (
            [category] => Appetizers
            [note] => Choose One Item From Each Category
        )

    [1] => Array
        (
            [category] => Flatbreads
            [note] => with Shoestring or Wedge Potatoes
        )

)

How would I be able to merge the ['note'] from Array #2 into Array #1, based on a match of the Array #1's category KEY with Array #2's 'category' VALUE? So the result is like this:

Array
(
    [Appetizers] => Array
        (
            [0] => Array
                (
                    [category] => Appetizers
                    [name] => Beef Tenderloin Carpaccio
                    [note] => Choose One Item From Each Category
                )

            [1] => Array
                (
                    [category] => Appetizers
                    [name] => Calamari Fritto Misto
                    [note] => Choose One Item From Each Category
                )    
        )

    [Flatbreads] => Array
        (
            [0] => Array
                (
                    [category] => Flatbreads
                    [name] => Quattro Formaggi
                    [note] => with Shoestring or Wedge Potatoes
               )

            [1] => Array
                (
                    [category] => Flatbreads
                    [name] => Tomato & Burrata
                    [note] => with Shoestring or Wedge Potatoes
                )

        )
)
Kerri
  • 1,211
  • 2
  • 15
  • 22
  • 2
    Assuming you don't need to support variable depth arrays, you could have a look at this SO question - http://stackoverflow.com/questions/1558291/php-merge-2-multidimensional-arrays – Scott S Jan 17 '13 at 22:45
  • Thanks, but yes, they do actually have variable depths. – Kerri Jan 18 '13 at 16:27

1 Answers1

0

Try this

$courses = array( 'Appetizers' => array( array( 'category' => 'Appetizers', 'name' => 'Beef Tenderloin' ), array( 'category' => 'Appetizers', 'name' => 'Calamari' ) ) );

$notes = array( array( 'category' => 'Appetizers', 'note' => 'Choose One Item From Each Category' ) );

function note( $key, $array ) {
    foreach( $array as $element )
        if( $element['category']  == $key )
            return $element['note'];
    return '';
}

foreach( $courses as $key => &$course ) {
    $course = array_map( function( $el ) use( $notes ) {
        $el['note'] = note( $el['category'], $notes );
        return $el;
    }, $course );
}
print_r( $courses );

It generates

Array
(
    [Appetizers] => Array
        (
            [0] => Array
                (
                    [category] => Appetizers
                    [name] => Beef Tenderloin
                    [note] => Choose One Item From Each Category
                )

            [1] => Array
                (
                    [category] => Appetizers
                    [name] => Calamari
                    [note] => Choose One Item From Each Category
                )

        )

)
C. E.
  • 10,297
  • 10
  • 53
  • 77