John Doe 2 John Doe 2 - 5 months ago 13
PHP Question

Php array replace 0 wth 00

I have an array which looks like this:

array:3 [▼
0 => "0"
1 => "01"
2 => "00"
]


I want to replace single zeros with two zeros.

should look like this:

array:3 [▼
0 => "00"
1 => "01"
2 => "00"
]


I did this:

$newDigit = str_replace("0", "00", $splitDigit);


But it added everywhere 2 zeros:

array:3 [▼
0 => "000"
1 => "0001"
2 => "0000"
]


How do I solve this

meh meh
Answer
foreach($arr as $str)
    $str = preg_replace('/^(0)$/', '00', $str);

or

foreach($arr as $str)
    if(strlen($str) === 1 && $str === '0')
            str = '00';

or

sprintf() that is told.