PHP将类似元素上的数组组合在一起


PHP combine arrays on similar elements

我有一些以下格式的数据:

even--heaped<br />
even--trees<br />
hardrocks-cocked<br />
pebble-temple<br />
heaped-feast<br />
trees-feast<br />

我想以一个输出结束,这样所有具有相同单词的行都可以相互添加,而不会重复。

even--heaped--trees--feast<br />
hardrocks--cocked<br />
pebbles-temple<br />

我尝试了一个遍历两个数组的循环,但这不是我想要的确切结果。对于数组$thing:

Array ( [0] => even--heaped [1] => even--trees [2] => hardrocks--cocked [3] => pebbles--temple [4] => heaped--feast [5] => trees--feast )

for ($i=0;$i<count($thing);$i++){
    for ($j=$i+1;$j<count($thing);$j++){
        $first = explode("--",$thing[$i]);
        $second = explode("--",$thing[$j]);
        $merge = array_merge($first,$second);
        $unique = array_unique($merge);
    if (count($unique)==3){
        $fix = implode("--",$unique);
        $out[$i] = $thing[$i]."--".$thing[$j];
    }
}
}
print_r($out);

但结果是:

Array ( [0] => even--heaped--heaped--feast [1] => even--trees--trees--feast [4] => heaped--feast--trees--feast )

这不是我想要的。有什么建议吗(对糟糕的variable names感到抱歉(。

这可能会对您有所帮助:

$in = array( 
    "even--heaped",
    "even--trees",
    "hardrocks--cocked",
    "pebbles--temple",
    "heaped--feast",
    "trees--feast"
);
$clusters = array();
foreach( $in as $item ) {
    $words = explode("--", $item);
    // check if there exists a word in an existing cluster...
    $check = false;
    foreach($clusters as $k => $cluster) {
        foreach($words as $word) {
            if( in_array($word, $cluster) ) {
                // add the words to this cluster
                $clusters[$k] = array_unique( array_merge($cluster, $words) );
                $check = true;
                break;
            }
        }
    }
    if( !$check ) {
        // create a new cluster
        $clusters[] = $words;
    }
}
// merge back
$out = array();
foreach( $clusters as $cluster ) {
    $out[] = implode("--", $cluster);
}
pr($out);

试试这个代码:

<?php
$data = array ("1--2", "3--1", "4--5", "2--6");
$n = count($data);
$elements = array();
for ($i = 0; $i < $n; ++$i)
{
      $split = explode("--", $data[$i]);
      $word_num = NULL;
      foreach($split as $word_key => $word)
      {
            foreach($elements as $key => $element)
            {
                  if(isset($element[$word]))
                  {
                        $word_num = $key;
                        unset($split[$word_key]);
                  }
             }
      }
      if(is_null($word_num))
      {
            $elements[] = array();
            $word_num = count($elements) - 1;
      }
      foreach($split as $word_key => $word)
      {
            $elements[$word_num][$word] = 1;
      }
}
//combine $elements into words
foreach($elements as $key => $value)
{
      $words = array_keys($value);
      $elements[$key] = implode("--", $words);
}
var_dump($elements);

它使用$elements作为散列数组,将单个唯一单词存储为关键字。然后组合按键以创建适当的单词。

打印此:

array(2) {
  [0]=>
  string(10) "1--2--3--6"
  [1]=>
  string(4) "4--5"
}

这里有一个带有简单控制流的解决方案。

<?php
    $things = array('even--heaped', 'even--trees', 'hardrocks--cocked', 
        'pebble--temple', 'heaped--feast' ,'trees--feast');
    foreach($things as $thing) {
        $str = explode('--', $thing);
        $first = $str[0];
        $second = $str[1];
        $i = '0';
        while(true) {
            if(!isset($a[$i])) {
                $a[$i] = array();
                array_push($a[$i], $first);
                array_push($a[$i], $second);
                break;
            } else if(in_array($first, $a[$i]) && !in_array($second, $a[$i])) {
                array_push($a[$i], $second);
                break;
            } else if(!in_array($first, $a[$i]) && in_array($second, $a[$i])) {
                array_push($a[$i], $first);
                break;
            } else if(in_array($first, $a[$i]) && in_array($second, $a[$i])) {
                break;
            }
            $i++;
        }
    }
    print_r($a);
?>

您似乎已经选择了user4035的答案作为最佳答案。

但我觉得这个是优化的(如果我错了,请纠正我(:eval.in

代码:

$array = Array ( 'even--heaped' , 'even--trees' ,'hardrocks--cocked' , 'pebbles--temple' , 'heaped--feast' , 'trees--feast' );
print "Input: ";
print_r($array);
for($j=0;$j < count($array);$j++){
    $len = count($array);
    for($i=$j+1;$i < $len;$i++){
        $tmp_array = explode("--", $array[$i]);
        $pos1 = strpos($array[$j], $tmp_array[0]);
        $pos2 = strpos($array[$j], $tmp_array[1]);
        if (!($pos1 === false) && $pos2 === false){
            $array[$j] = $array[$j] . '--'.$tmp_array[1];unset($array[$i]);
        }elseif(!($pos2 === false) && $pos1 === false){
            $array[$j] = $array[$j] . '--'.$tmp_array[0];unset($array[$i]);
        }elseif(!($pos2 === false) && !($pos1 === false)){
            unset($array[$i]);
        }
    }
    $array = array_values($array);
}
print "'nOutput: ";
print_r($array);