Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP数组中的数据形成新字符串_Php_Arrays - Fatal编程技术网

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

使用PHP数组中的数据形成新字符串,php,arrays,Php,Arrays,我需要减少这些数字的数量,并以更简洁的方式呈现它们,而不是以相同的“前缀”或“根”呈现几行数字。例如: 如果我有一个这样的数组,有几个数字字符串(obs:仅数字,数组已排序): 字符串:123456在数组的所有元素中都是相同的,因此它将是数字的根或前缀。根据上面的数组,我将得到如下结果: //The numbers in brackets represent the sequence of the following numbers, //instead of showing the rows,

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

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

字符串: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]";

有什么想法吗?

嗯,据我所知,您希望最后的字符串具有唯一字符。(我不确定您是否需要订购) 因此,首先内爆来创建字符串

$stringFormed = implode("", $array);
然后我们得到独特的字符:

$stringFormed=implode("",array_unique(str_split($stringFormed)));
输出:123456789
    $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]";
"12345647", "12345648", "12345649", "12345657", "12345658", "12345659", ); //查找所有元素的公共字符串位置 $res=array(); foreach($arr形式的数组){
对于($i=0;$i,作为第一个示例的解决方案,但我认为可能没有几个根。 顺便说一下,我不确定它的编码是否正确

<?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;
?>


[x-y]是因为x是少的,y是最多的?如果一些数字不存在怎么办?请详细说明你的问题。问题真的不是建设性的,可能这篇文章应该帮助你找到字符串的开头==>或者这个==>是的,谢谢@gilles emmanuel,这很有帮助!然后我需要一个线索来获取括号中的数字。
<?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;
?>