0

some "simple" problem:

I've this array;

$myArray = array(
'FOO',
'BAR,
);

i want :

$mayArray = array(
   'FOO' => array(),
   'BAR' => array(),
);

in the moment iam doing it with an foreach:

foreach ($myArray as $key => $val) {
    $newArray[$val] = array();
}

$myArray = $newArray;

is there an easyer way ? ;-)

2 Answers 2

5

The way you have is pretty easy to understand. But you can also do this:

 $myArray = array_fill_keys($myArray, array());

Docs here: https://www.php.net/manual/en/function.array-fill-keys.php

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

Comments

4

You can use array_fill_keys, here is example:

$myArray = array_fill_keys($myArray, array());

Comments

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.