如何从PHP中的多维数组在自定义数组中创建组合

时间:2018-09-03 05:07:14

标签: php arrays multidimensional-array

我是数组的新手实际上我想从多维数组创建一个自定义数组。我有下面这样的数组:-

Array
(
[0] => Array
    (
        [0] => 30
        [1] => 31
    )

[1] => Array
    (
        [0] => 4
        [1] => 5
        [2] => 32
    )

[2] => Array
    (
        [0] => 29
    )

)
$filterids = $getfilterids; // that is saved in one variable.

现在只有三个数组,将来可能会更多,这意味着任何数量的数组。所以我想与另一个elemnets数组进行组合,然后需要用comma(,)内爆。现在我想输出类似这样的东西:-

Array
(
    [0] => 30,4,29
    [1] => 30,5,29
    [2] => 30,32,29
    [3] => 31,4,29
    [4] => 31,5,29
    [5] => 31,32,29
)

我已搜索但无法开始。请帮我如何做这种功能。 注意:-数组可以更多,它应该是动态的。预先感谢。

4 个答案:

答案 0 :(得分:1)

    function combine($arr){
        $ret = array_pop($arr);
        for($i = count($arr) - 1; $i >= 0; $i--){
            $ret2 = [];
            foreach ($arr[$i] as $ar){
                foreach ($ret as $rt){
                    $ret2[] = $ar . "," . $rt;
                }
            }
            $ret = $ret2;
        }
        return $ret;
    }
    combine($filtered);

O(nm ** 2)n是数组的长度,m是其最长元素的长度

答案 1 :(得分:0)

最后从Google到处搜索。终于我找到了解决方案。希望以后能对某人有所帮助:-

function combinations($arrays, $i = 0) {    
    if (!isset($arrays[$i])) {
        return array();
    }
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }
    $tmp = combinations($arrays, $i + 1);
    $result = array();
    foreach ($arrays[$i] as $v) {
        foreach ($tmp as $t) {
            $result[] = is_array($t) ? 
                implode(',',array_merge(array($v), $t)) :
                implode(',',array($v,$t));
        }
    }
    return $result;
}

$result = combinations($filterids);   // call the function
echo "<pre>"; print_r($result);    // echo the expected output 

答案 2 :(得分:0)

您可以开始将这个问题视为输入中数组的“笛卡尔积”,然后将格式化函数应用于笛卡尔积。笛卡尔积的算法是众所周知的,您可以在这里参考:https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists

我建议使用通用的“笛卡尔积”函数,您可以在代码中重复使用。

以下是建议的解决方案:

<?php
$input = Array
(
0 => Array
    (
        0 => 30,
        1 => 31
    ),

1 => Array
    (
        0 => 4,
        1 => 5,
        2 => 32
    ),

2 => Array
    (
        0 => 29
    )

);


$expected = Array
(
    0 => "30,4,29",
    1 => "30,5,29",
    2 => "30,32,29",
    3 => "31,4,29",
    4 => "31,5,29",
    5 => "31,32,29"
);

//make an intermediate array with the cartesian product of the $input array

$intermediate = cartesian($input);

// and then apply the "implode" function to format the output as requested.

$output = array_map(function($v) {
    return implode(",", $v);
}, $intermediate);


var_dump($output);

if( ($output === $expected)) {
    echo "output is equal to expected" .PHP_EOL;
} else {
    echo "not equals" .PHP_EOL;
}


function cartesian($input) {
    $result = array(array());

    foreach ($input as $key => $values) {
        $append = array();

        foreach($result as $product) {
            foreach($values as $item) {
                $product[$key] = $item;
                $append[] = $product;
            }
        }

        $result = $append;
    }

    return $result;
}
产生此输出的

array(6) {
  [0]=>
  string(7) "30,4,29"
  [1]=>
  string(7) "30,5,29"
  [2]=>
  string(8) "30,32,29"
  [3]=>
  string(7) "31,4,29"
  [4]=>
  string(7) "31,5,29"
  [5]=>
  string(8) "31,32,29"
}
output is equal to expected

答案 3 :(得分:-1)

希望这会有所帮助

foreach ($filterids as $val) {
    $new_val[] = implode(',', $val);
}
print_r($new_val);

$filterids将是多维数组。

输出:https://eval.in/1053282