Php array clear

Is there a default function to clear only array values . For ex arr {10,3,3,34,56,12} arr. {0,0,0,0,0,0}

+4
source share
3 answers
$array = array_combine(array_keys($array), array_fill(0, count($array), 0)); 

Alternative:

 $array = array_map(create_function('', 'return 0;'), $array); 
+8
source

To answer your original question: No, there is no default PHP function for this. However, you can try some combinations of other functions described by other guys. However, I find the following code fragment more readable:

 $numbers = Array( "a" => "1", "b" => 2, "c" => 3 ); foreach ( $numbers as &$number ) { $number = 0; } 
+3
source
 $array = array_fill(0, count($array), 0); 

This creates an array of original size filled with zeros.

+1
source

Source: https://habr.com/ru/post/1311381/


All Articles