0

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
  • And wouldn't array_flip suffice? Use a foreach to create subarrays. Commented Dec 24, 2013 at 3:32

1 Answer 1

4
$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

Sign up to request clarification or add additional context in comments.

2 Comments

This has the disadvantage of removing any associative indexes the array had
@IWantAnswers It's not a "disadvantage", it's exactly what the OP asked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.