PHP算法:如何获得所有排列和组合(数组嵌套)

时间:2017-12-25 02:30:08

标签: php algorithm

现在我有了一个二维数组:$ data;

//aTicker.java
@Entity 
public class aTicker extendstTicker {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

}

//tTicker.java
@Entity
public class tTicker {    
    private  String currencyPair;
    private  BigDecimal open;
    private  BigDecimal last;

    ......
}

我会得到所有的排列和组合 (每个键每个单元只产生一个元素)

$data[0][] = "AAA";
$data[0][] = "aaa";
$data[0][] = "AaA";
$data[1][] = "BBB";
$data[1][] = "bbb";
$data[1][] = "BbB";
$data[2][] = "CCC";
$data[2][] = "ccc";
$data[2][] = "CcC";

现在$ data的键很少(只有三个键),我可以通过代码写入实现:

example: 
"AAA-BBB-CCC" is right
"AAA-aaa-BBB" is error,because AAA and aaa in the same key

结果:

foreach($arr[0] as $value)
{
    foreach($arr[1] as $val)
    {
        foreach($arr[2] as $v)
        {
            echo $value.'-'.$val.'-'.$v;
            echo '<br>';
        }
    }
}

但是,如果$ data的关键是更多(例子30),我不能写30个foreach ... 所以我需要一个算法 非常感谢你

3 个答案:

答案 0 :(得分:0)

function merge($data){
  $datas = [];
  foreach($data as $value)
   {
    for($i = 0; $i < count($value); $i++)
    {
        @$datas[$i] .= $value[$i] . '-';
    }
   }
  foreach($datas as $i=>$d)
   {
      $datas[$i] = trim($d, '-');
   }
   return $datas;
}


$data[0][] = "aaa";
$data[0][] = "AaA";
$data[1][] = "BBB";
$data[1][] = "bbb";
$data[1][] = "BbB";
$data[2][] = "CCC";
$data[2][] = "ccc";
$data[2][] = "CcC";
$data[3][] = "DDD";
$data[3][] = "ddD";
$data[3][] = "DdD";


print_r(merge($data));

结果是

Array
  (
    [0] => AAA-BBB-CCC-DDD
    [1] => aaa-bbb-ccc-ddD
    [2] => AaA-BbB-CcC-DdD
 )

到了你想要的,你可以做到

foreach(merge($data) as $item)
{
  echo $item;
  echo "<br />";
}

答案 1 :(得分:0)

我发现了两种方法 之一:

$data = [
    ['AAA','aaa','aAa'],
    ['BBB','bbb','bBb'],
    ['CCC','ccc','cCc'],
];
dictionariesIterator($data,$list);
var_dump($list);

function dictionariesIterator($data,&$list,$length = 0)
{
    if(count($data) <= $length)
    {
        return null;
    }
    if(empty($list))
    {
        $list = $data[$length];
    }
    else
    {
        $new = [];
        foreach($list as $value)
        {
            foreach($data[$length] as $value1)
            {
                $new[] = $value.'-'.$value1;
            }
        }
        $list = $new;
    }
    dictionariesIterator($data,$list,$length + 1);
}

二:

$result = [];
foreach (array_keys($data) as $k) {
    $tmp = [];
    if (empty($result)) {
        $result = $data[$k];
    } else {
        foreach ($result as $item1) {
            foreach ($data[$k] as $item2) {
                $tmp[] = $item1 . '-' . $item2;
            }
        }
        $result = $tmp;
    }
}
var_dump($result);

答案 2 :(得分:0)

<?php
$data[0][] = "AAA";
$data[0][] = "aaa";
$data[0][] = "AaA";
$data[1][] = "BBB";
$data[1][] = "bbb";
$data[1][] = "BbB";
$data[2][] = "CCC";
$data[2][] = "ccc";
$data[2][] = "CcC";
$data[2][] = "ccC";
$data[3][] = "DDD";

function tab($table, array $result = null) {
    if (empty($table)) {
        return $result;
    }

    $row = array_shift($table);
    $result = row($row, $result, []);

    return tab($table, $result);
}


function row($row, array $list, array $result = null) {
    if (empty($row)) {
        return $result;
    }

    $value = array_shift($row);
    if (empty($list)) {
        $result[] = [$value];
    } else {
        foreach ($list as $item) {
            $result[] = array_merge($item, [$value]);
        }
    }

    return row($row, $list, $result);
}

function fmt($rows, $result = null) {
    if (empty($rows)) {
        return $result;
    }
    $row = array_shift($rows);
    $result .= implode("-", $row).PHP_EOL;
    return fmt($rows, $result);
}

echo fmt(tab($data, []));