使用数组PHP中的数据形成一个新字符串

时间:2012-07-23 09:28:46

标签: php arrays

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

如果我有这样的数组,有几个数字串(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]";

有什么想法吗?

3 个答案:

答案 0 :(得分: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]";

答案 1 :(得分:0)

好吧,据我所知,你想要带有唯一字符的最终字符串。 (我不确定你是否要订购) 所以,首先内爆创建字符串

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

然后我们得到了独特的字符:

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

输出:123456789

答案 2 :(得分: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;
?>