我什至不知道该用什么词:)我有一个数组:array( 0 => array( 'protein' => 'proteiny', 'total_fat' => 'total faty', 'carbohydrates' => 'carbohydrates', 'food_energy' => 'food energy',...另一个数组:1 => array( 'protein' => 'grams', 'total_fat' => 'grams', 'carbohydrates' => 'grams', 'food_energy' => 'kcals',...我想在这个结构中将它们组合成一个新数组:$newone = array( array('slug' => 'protein', 'name' => 'proteiny', 'format' => 'grams'), array('slug' => 'total_fat', 'name' => 'total faty', 'format' => 'grams'), array('slug' => 'carbohydrates', 'name' => 'carbohydrates', 'format' => 'grams'), array('slug' => 'food_energy', 'name' => 'food energy', 'format' => 'kcals'), ...有没有办法做到这一点?
2 回答
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
您可以在具有范围use($b)的数组上使用array_map( )来制作. 例子:$a$newone
$a = array('protein' => 'proteiny', 'total_fat' => 'total faty', 'carbohydrates' => 'carbohydrates', 'food_energy' => 'food energy');
$b = array('protein' => 'grams', 'total_fat' => 'grams', 'carbohydrates' => 'grams', 'food_energy' => 'kcals');
$newone = array_map(function($val, $slug) use ($b) {
return array('slug' => $slug, 'name' => $val, 'format' => $b[$slug]);
}, $a, array_keys($a));
print_r($newone);
MYYA
TA贡献1868条经验 获得超4个赞
foreach当$a第一个数组$b是第二个数组时,简单循环如何:
foreach($a as $k => $e) {
$res[] = array('slug' => $k, 'name' => $e, 'format' => $b[$k]);
}
现场示例:3v4l
- 2 回答
- 0 关注
- 134 浏览
添加回答
举报
0/150
提交
取消