douxiangdiao6348
douxiangdiao6348
2013-12-24 03:29

使用array_map修改数组php

已采纳

How to modify the following array using array map. So far I couldn't achieve the wanted results so here is my question:

Convert this:

Array
(
    [Open+Sans:300,300italic,regular,italic,600,600italic,700,700italic,800,800italic] => Open Sans
    [Roboto:100,100italic,300,300italic,regular,italic,500,500italic,700,700italic,900,900italic] => Roboto
    [Oswald:300,regular,700] => Oswald
)

into

Array
(
  array('label' => 'Open Sans','value' => 'Open+Sans:300,300italic,regular,italic,600,600italic,700,700italic,800,800italic'),
  array('label' => 'Roboto','value' => 'Roboto:100,100italic,300,300italic,regular,italic,500,500italic,700,700italic,900,900italic'),
  array('label' => 'Oswald','value' => 'Oswald:300,regular,700'),
);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dousuohe5882 dousuohe5882 8年前
    $data = array(
        'foo' => 'bar',
        'baz' => 'bla',
    );
    
    $result = array_map(
        function($key, $value) {
            return array(
                'label' => $key,
                'value' => $value,
            );
        },
        array_keys($data),
        array_values($data)
    );
    
    var_dump($result);
    

    Online demo: http://ideone.com/95DCuf

    点赞 评论 复制链接分享

相关推荐