$data = array(
'123456',
'123457'
);
$new = array();
$data_tmp = $new;
while (1) {
$test = array();
$new = array();
foreach ($data_tmp as $item) {
if (preg_match('/^\d{2,}/',$item) ) {
$suffix = substr($item,0,length($item)-1);
$test[$item][$suffix]+=1;
}
}
foreach (array_keys($test) as $base) {
if (count(array_keys($test[$base])) == 10) {
array_push($new, $base);
}
else {
foreach (array_keys($test[$base]) as $k) {
array_push($new, $base . $k);
}
}
if (count($new) == count($data_tmp)) {
goto leavewhile;
}
$data_tmp = $new;
}
leavewhile:
foreach (sort($new) as $val) {
echo $val . "\n";
}