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
PHP在相似的元素上组合数组_Php_Arrays_Merge_Unique_Combinations - Fatal编程技术网

PHP在相似的元素上组合数组

PHP在相似的元素上组合数组,php,arrays,merge,unique,combinations,Php,Arrays,Merge,Unique,Combinations,我有一些这种格式的数据: even--heaped<br /> even--trees<br /> hardrocks-cocked<br /> pebble-temple<br /> heaped-feast<br /> trees-feast<br /> 这不是我想要的。任何建议(对于糟糕的变量名)都很抱歉。这可能会帮助您: $in = array( "even--heaped", "even--tr

我有一些这种格式的数据:

even--heaped<br />
even--trees<br />
hardrocks-cocked<br />
pebble-temple<br />
heaped-feast<br />
trees-feast<br />
这不是我想要的。任何建议(对于糟糕的
变量名
)都很抱歉。

这可能会帮助您:

$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);

下面是一个具有简单控制流的解决方案

<?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
的答案作为最佳答案

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

代码:

$array=array('even--heaped'、'even--trees'、'hardlocks--cocked'、'pebbles--temple'、'heaped--fest'、'trees--fest');
打印“输入:”;
打印(数组);
对于($j=0;$j
请提供$thing arrayArray([0]=>偶数--堆[1]=>偶数--树[2]=>硬石--倒立[3]=>卵石--庙宇[4]=>堆--宴席[5]=>树--宴席)我使用了相同的方法,但是哈希而不是数组。检查散列键比为每次迭代执行array_unique/array_merge更有效。@user2426240使用unset而不是将元素标记为NULL,从而稍微改进了答案
$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);
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);
?>
$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);