2012-07-23 66 views
0

我需要減少這些數字的數量並以更簡潔的方式呈現它們,而不是用相同的「前綴」或「根」來呈現多行數字。例如:使用數組中的數據形成一個新的字符串PHP

如果我有一個這樣的陣列,以數字的幾串(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]"; 

有什麼想法?

+0

[X-Y]是x是少,y爲的是什麼?如果有些數字不存在會怎麼樣? – 2012-07-23 09:34:49

+0

請詳細說明您的問題。 – 2012-07-23 09:36:37

+0

問題確實不是建設性的 – diEcho 2012-07-23 09:37:07

回答

0

嗯,據我所知,你想要最後一個字符串與唯一字符。 (我不知道如果你想訂購) 所以,首先破滅創建的字符串

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

然後我們得到的唯一字符:

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

OUTPUT:123456789

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]"; 
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; 
?> 
相關問題