Petala
Petala

Reputation: 21

Form a new string with data from an array PHP

I would need to reduce the quantity of these numbers and present them in a more concise way, instead of presenting several lines of numbers with the same "prefix" or "root". For example:

If I have an array like this, with several strings of numbers (obs: only numbers and the array is already sorted):

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

The string: 123456 is the same in all elements of the array, so it would be the root or the prefix of the number. According to the above array I would get a result like this:

//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]"; 

Another example:

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

From the second array, I should get a result like this:

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

Any idea?

Upvotes: 0

Views: 139

Answers (3)

gilles emmanuel
gilles emmanuel

Reputation: 221

That as a solution for first example but i didn't thought there could be several roots. By the way i'm not sure it's well coded...

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

Upvotes: 0

Mwayi
Mwayi

Reputation: 1623

    $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]";

Upvotes: 1

Stoia Alex
Stoia Alex

Reputation: 161

Well, as I understand you want the final string with unique characters. (i'm not sure if you want it ordered) So, first implode to create the string

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

Then we get the unique chars :

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

OUTPUT: 123456789

Upvotes: 0

Related Questions