Is there any default function to clear only the values of an array?
For example:
$array = [
10,
3,
3,
34,
56,
12
];
Desired result:
[
0,
0,
0,
0,
0,
0
]
$array = array_combine(array_keys($array), array_fill(0, count($array), 0));
Alternative:
$array = array_map(create_function('', 'return 0;'), $array);
To answer your original question: No, there isn't any default PHP function for this. However, you can try some combination of other functions as other guys described. However, I find following piece of code more readable:
$numbers = Array( "a" => "1", "b" => 2, "c" => 3 );
foreach ( $numbers as &$number ) {
$number = 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