数组中的组对

时间:2013-11-15 03:50:49

标签: php arrays grouping

我正在尝试将关系中的航空公司划分为单一链条。

Array
(
    [0] => Array
        (
            [0] => Aeroflot
            [1] => S7
            [2] => Transaero
        )

    [1] => Array
        (
            [0] => Alitalia
            [1] => Lufthansa
        )

    [2] => Array
        (
            [0] => Transaero
            [1] => United
        )

    [3] => Array
        (
            [0] => United
            [1] => Alitalia
        )

    [4] => Array
        (
            [0] => Volotea
            [1] => Iberia
        )

    [5] => Array
        (
            [0] => Transaero
            [1] => Aeroflot
        )

)

从那个数组我需要找到元素之间的连接并将它组合成组。预期结果:

Array
(
    [0] => Array
        (
            [0] => Aeroflot
            [1] => S7
            [2] => Transaero
            [3] => United
            [4] => Alitalia
            [5] => Lufthansa
        )

    [1] => Array
        (
            [0] => Volotea
            [1] => Iberia
        )

)

任何人都可以帮忙吗?我已经尝试了十几种方法,但仍然没有成功。

我尝试过的最接近的方式有效,但并非在所有情况下都有效:

    function array_searchRecursive($needle,$haystack) {
        foreach($haystack as $key=>$value) {
            $current_key=$key;
            if($needle===$value OR (is_array($value) && array_searchRecursive($needle,$value) !== false)) {
                return $current_key;
            }
        }
        return false;
    }   

foreach ($newarr as $key => $airlines)
    {
        foreach ($airlines as $lastkey => $airline)
        {
        $index = array_searchRecursive($airline,$newarr);   
        echo $airline.$index."\n";
        if ($index !== false)
        {
            $newarr[$index] = array_merge($newarr[$index],$airlines);
            $lastarr[] = $index;
        }

        }
    }

但它与数组中的所有值都不匹配。

3 个答案:

答案 0 :(得分:0)

根据您的示例,您只需将第一个子数组作为参考对子数组进行分组。例如,如果您在第一个子数组和后续子数组中有任何共同的元素,那么您将它们组合成一个子数组。

<?php

$arr = array(
array('a', 'b', 'c', 'd'),
array('d', 't'),
array('t', 'f'),
array('k', 'o'),
array('p', 'z')
);
$arr_implode = array();

foreach ($arr as $key => $value) {
 if (is_array($value)) {
$arr_implode[$key] = implode('', $value);
} else {
$arr_implode[$key] = $value;
}
}

$arr_key = array();
$result = array();
$count = count($arr_implode);
$tempj = 0;
for ($i = 0; $i <= $count; $i++) {
  $flag = FALSE;
  for ($j = ($i + 1); $j < $count; $j++) {
  similar_text($arr_implode[$i], $arr_implode[$j], $percent);
   if ($percent > 0) {
    $result[] = array_merge($arr[$i],$arr[$j]);
    break;
  } else {
    $result[] = $arr[$j];
    break;
 }
}

}
foreach($result as $key => $val){
  $result[$key] = array_unique($val);
}
echo "<pre>";
print_r($result);
echo "</pre>";
?>

试试这段代码。

答案 1 :(得分:0)

递归功能会帮助你。欢迎你)
    

$arr = array(
    array('Aeroflot','S7','Transaero'),
    array('Alitalia','Lufthansa'),
    array('Transaero','United'),
    array('United','Alitalia'),
    array('Volotea','Iberia'),
    array('Transaero','Aeroflot')
);

function getConnections($arr,$curr_line_n=0,$num=0) {
    for($i=0;$i<count($arr[$curr_line_n]);$i++) {
        $cur_air_name = $arr[$curr_line_n][$i];

        for($k=$curr_line_n+1; $k<count($arr); $k++) {

            for($l=0;$l<count($arr[$k]);$l++) {

                if ($arr[$k][$l]==$cur_air_name) {
                    $arr[$curr_line_n] = array_values(array_unique(array_merge($arr[$curr_line_n],$arr[$k])));
                    array_splice($arr,$k,1);
                    $num++;
                    $arr = getConnections($arr,$curr_line_n,$num);

                }
            }
        }
    }
    $num++;
    $curr_line_n++;
    if ($curr_line_n!=count($arr)) {
        $arr = getConnections($arr,$curr_line_n,$num);
    }


    return $arr;
}
print_r(getConnections($arr));

答案 2 :(得分:0)

   $arr = [
      ['Aeroflot', 'S7', 'Transaero'],
      ['Alitalia', 'Lufthansa'],
      ['Transaero', 'United'],
      ['United', 'Alitalia'],
      ['Volotea', 'Iberia'],
      ['Transaero', 'Aeroflot']
     ];
  $hash = [];
  $result = [];
  foreach($arr as $set){
    foreach($set as $el){
      if(!$hash[$el]) $hash[$el] = [] ;
      $hash[$el] = array_merge($hash[$el], $set);
    }
  }
  function merge_connections(&$h, $key){    
    if(!$h[$key]) return [];
    $data = [$key];
    $rels = $h[$key];
    unset($h[$key]);
    foreach($rels as $rel){
      if($rel==$key) continue;
      $data = array_merge($data, merge_connections($h, $rel));
    }    
    return $data;
  }
  foreach(array_keys($hash) as $company){   
   if(!$hash[$company]) continue;   
   array_push($result, merge_connections($hash, $company));
  }
  print_r($result);