How to generate an associative array in PHP?

This code
$m1 = array('one', 'two', 'three');
$m2 = array();

for ($j = 0; $j < count($m1); $j++) {
 $ready = array("$m1[$j]"=>0);
 $m2[] = $ready;
}

Generates an array of the form:
array(3) {
[0]=>
 array(1) {
["one"]=>
int(0)
}
[1]=>
 array(1) {
["two"]=>
int(0)
}
[2]=>
 array(1) {
["three"]=>
int(0)
}
}


And I would like this:
array(3) {
["one"]=>
int(0)
["two"]=>
int(0)
["three"]=>
int(0)
}
July 12th 19 at 16:52
2 answers
July 12th 19 at 16:54
Solution
$m1 = ['one', 'two', 'three'];
$m2 = [];

foreach ($m1 as $v) {
 $m2[$v] = 0;
}
print_r($m2);

Array
(
 [one] => 0
 [two] => 0
 [three] => 0
)
July 12th 19 at 16:56
$m1 = array('one', 'two', 'three');
$m2 = array_combine($m1, array_fill(0, count($m1), 0));

Find more questions by tags PHP