I have the following array:
array(
'elem1', 'elem2', 'elem3'
);
I want to have the following:
array(
'elem1' => 0,
'elem2' => 0,
'elem3' => 0
);
is this possible with array_fill
? I cant see that it is.
If not, is there a way to do this without iterating over the array?
Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.
in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.
You can use the PHP array_values() function to get all the values of an associative array.
Yup.. Try this
<?php
$keys = array('elem1', 'elem2', 'elem3');
$a = array_fill_keys($keys, 0);
print_r($a);
?>
Output:
array(
'elem1' => 0,
'elem2' => 0,
'elem3' => 0
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With