0

我需要减少这些数字的数量并以更简洁的方式呈现它们,而不是呈现具有相同“前缀”或“根”的几行数字。例如:

如果我有一个这样的数组,有几个数字字符串(obs:只有数字并且数组已经排序):

$array = array( 
"12345647",
"12345648",
"12345649",
"12345657",
"12345658",
"12345659",
);

字符串:123456 在数组的所有元素中都是相同的,因此它是数字的根或前缀。根据上面的数组,我会得到这样的结果:

//The numbers in brackets represent the sequence of the following numbers,
//instead of showing the rows, I present all the above numbers in just one row:
$stringFormed = "123456[4-5][7-9]"; 

另一个例子:

$array2 = array( 
"1234",
"1235",
"1236",
"1247",
"2310",
"2311",
);

从第二个数组,我应该得到这样的结果:

$stringFormed1 = "123[4-7]";
$stringFormed2 = "1247";
$stringFormed3 = "231[0-1]";

任何想法?

4

3 回答 3

1
    $array = array( 
    "12345647",
    "12345648",
    "12345649",
    "12345657",
    "12345658",
    "12345659",
    );

    //find common string positions for all elements

    $res = array();
    foreach($array as $arr){

        for($i=0;$i<strlen($arr);$i++){

            $res[$i][$arr[$i]] = $arr[$i];
        }

    }
    //make final string
    foreach($res as $pos){
        if(count($pos)==1)
        $str .= implode('',$pos);
        else{
//u may need to sort these values if you want them in order
            $end = end($pos);
            $first = reset($pos); 
            $str .="[$first-$end]";
        }
    }

echo $str; // "123456[4-5][7-9]";
于 2012-07-23T10:16:50.230 回答
0

作为第一个示例的解决方案,但我不认为可能有多个根源。顺便说一句,我不确定它是否编码良好......

<?php
function longest_common_substring($words)
{
  $words = array_map('strtolower', array_map('trim', $words));
  $sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
  usort($words, $sort_by_strlen);
  // We have to assume that each string has something in common with the first
  // string (post sort), we just need to figure out what the longest common
  // string is. If any string DOES NOT have something in common with the first
  // string, return false.
  $longest_common_substring = array();
  $shortest_string = str_split(array_shift($words));
  while (sizeof($shortest_string)) {
    array_unshift($longest_common_substring, '');
    foreach ($shortest_string as $ci => $char) {
      foreach ($words as $wi => $word) {
        if (!strstr($word, $longest_common_substring[0] . $char)) {
          // No match
          break 2;
        } // if
      } // foreach
      // we found the current char in each word, so add it to the first longest_common_substring element,
      // then start checking again using the next char as well
      $longest_common_substring[0].= $char;
    } // foreach
    // We've finished looping through the entire shortest_string.
    // Remove the first char and start all over. Do this until there are no more
    // chars to search on.
    array_shift($shortest_string);
  }
  // If we made it here then we've run through everything
  usort($longest_common_substring, $sort_by_strlen);
  return array_pop($longest_common_substring);
}

$array = array( 
"12345647",
"12345648",
"12345649",
"12345657",
"12345658",
"12345659",
);
$result= longest_common_substring($array);
for ($i = strlen($result); $i < strlen($array[0]); $i++) {
    $min=intval($array[0][$i]);
    $max=$min;
    foreach ($array as $string) {
        $val = intval($string[$i]);
        if($val<$min)
            $min=$val;
        elseif($val>$max)
            $max=$val;
}
    $result.='['.$min.'-'.$max.']';
}
echo $result;
?>
于 2012-07-23T10:18:42.377 回答
0

好吧,据我了解,您想要具有唯一字符的最终字符串。(我不确定您是否要订购)所以,首先 implode 创建字符串

$stringFormed = implode("", $array);

然后我们得到唯一的字符:

$stringFormed=implode("",array_unique(str_split($stringFormed)));

输出:123456789

于 2012-07-23T09:50:14.963 回答