0

配列#1は次のようになります(簡略化):

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
                )

        )
)

アレイ#2は次のようになります。

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

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

)

配列#1のカテゴリKEYと配列#2の'カテゴリ'VALUEの一致に基づいて、['note']を配列#2から配列#1にマージするにはどうすればよいですか?したがって、結果は次のようになります。

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
                )

        )
)
4

1 に答える 1

0

これを試して

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

生成します

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
                )

        )

)
于 2013-01-17T23:20:43.377 に答える