0

0I have this string:

01, 02, 03, 04, 05, 06, 07, 08, 09

and I need to convert it in an associative array with the numbers as keys and the values set to 0

Array(
   "01" => 0,
   "02" => 0,
   etc
)

I've found the function array_walk but if I try to use it: http://phpfiddle.org/main/code/5z2-bar

$string = "01, 02, 03, 04, 05, 06, 07, 08, 09";
$days = explode(",", $string);

$assDays = Array();

function associate($element) 
{
    $assDays[$element] = 0;
}

echo "<pre>";
print_r(array_walk($days, 'associate'));
echo "</pre>";

Doesn't work. I'm sure the problem is that I don't pass the values to the function associate but I don't know how to do.

0

1 Answer 1

3

Use array_fill_keys:

$assDays = array_fill_keys($days, 0);
Sign up to request clarification or add additional context in comments.

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.