How to create combinations in custom array from multidimensional array in php

Viewed 395

I am new in arrays Actually i want create one custom array from multidimensional array. I have Arrays like this below:-

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.

Right now there are only three arrays may be it will be more in future means it would any number of arrays. So i want to make combinations with another elemnets Array and after that need to implode with comma(,). Now i want output something like this :-

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
)

I have searched but unable to start. Please help me how can i do this type of functionality. Note :- Arrays can be more it should be dynamic. Thanks in advance.

4 Answers
    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 is the length of the array, m is the lenght of its longest element

Finally search around from google. Finally i find the solution. Hope it will help someone in future :-

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 

You can start thinking at this problem as a "cartesian product" of the arrays in the input and than apply a formatting function to the cartesian product. Algorithm for the cartesian product are well known, you can take a look here for reference: https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists

I suggest to use a generic "cartesian product" function that you can reuse in your code.

Here the proposed solution:

<?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;
}

that produces this output:

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

Hope this helps

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

$filterids will be the multidimensional array.

Output:https://eval.in/1053282

Related