数组#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
)
)
如何根据Array#1的KEY类别与Array#2的'category'VALUE的匹配,如何将Array#2中的['note']合并到Array#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
)
)
)
答案 0 :(得分: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
)
)
)