$value){ $match = preg_match("/3/",$value); if($value % 3 == 0 or $match == true){ $answer[] = $value; } } asort($answer); foreach($answer as $key => $value){ echo $value."\n"; }