使用array_map修改数组php

时间:2013-12-24 03:29:09

标签: php array-map

如何使用数组映射修改以下数组。到目前为止,我无法达到想要的结果所以这是我的问题:

转换它:

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
)

进入

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 个答案:

答案 0 :(得分:3)

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

在线演示:http://ideone.com/95DCuf