Search code examples
phparraysreplacedefault-valuefill

Replace all values in a flat array with 0


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
]

Solution

  • $array = array_combine(array_keys($array), array_fill(0, count($array), 0));
    

    Alternative:

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