php - Explode array and set as associative with default values? -
0i have string:
01, 02, 03, 04, 05, 06, 07, 08, 09
and need convert in associative array numbers keys , values set 0
array( "01" => 0, "02" => 0, etc )
i've found function array_walk if try 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 problem don't pass values function associate don't know how do.
use array_fill_keys:
$assdays = array_fill_keys($days, 0);
Comments
Post a Comment