php - How to convert a string of numbers into an array? -
i have string e.g. 1398723242
. trying check string , odd numbers out in case 13973
.
i facing problem on how put string array. after putting string array know have loop through array, this:
foreach($array $value){ if($value % 2 !== 0){ echo $value; } }
so can body please on first part on "how put above string array can evaluate each digit in above loop?"
this should work you:
just use str_split()
split string array. can use array_filter()
filter numbers out. e.g.
<?php $str = "1398723242"; $filtered = array_filter(str_split($str), function($v){ return $v & 1; }); echo implode("", $filtered); ?>
output:
13973
Comments
Post a Comment