2013-12-24 56 views
0

如何使用數組映射修改以下數組。到目前爲止,我也無法達到想要的結果,所以這裏是我的問題:使用array_map修改array array php

轉換這樣的:

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'), 
); 
+0

而止跌't ['array_flip'](http://php.net/array_flip)就夠了嗎?使用['foreach'](http://php.net/foreach)來創建子數組。 – mario

回答

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