2013-05-27 60 views
4

陣列我有這種格式的一些數據:PHP結合上相似的元素

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 /> 

我試過一個循環經過兩個數組,但它並不是我想要的確切結果。一個數組$件事:

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抱歉)。

+0

請提供$件事陣列 – user4035

+0

陣列([0] =>甚至 - heapped [1] => even - trees [2] => hardrocks - 豎起[3] =>鵝卵石 - 寺廟[4] =>堆滿 - 盛宴[5] =>樹 - 宴餐) – user2426240

回答

2

試試這個代碼:

<?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" 
} 
+0

是的!驚人。完美的作品。非常感謝。 – user2426240

+0

@ user2426240通過使用unset代替標記元素作爲NULL改進了答案 – user4035

3

這可以幫助你:

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

我使用了相同的方法,但使用了散列而不是數組。檢查散列鍵更有效,然後對每個迭代執行array_unique/array_merge。 – user4035

1

下面是一個簡單的控制流程中的溶液。

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

看來你已經選擇了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);