Home > Enterprise >  need to make string from combination of array items from multidimensional array in php
need to make string from combination of array items from multidimensional array in php

Time:01-06

I have an array like below:

Array
(
  [0] => Array
    (
      [0] => one
      [1] => Array
        (
          [0] => a
          [1] => b
          [2] => c
        )
    )

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

  [2] => Array
    (
      [0] => three
      [1] => Array
        (
          [0] => 7
          [1] => 8
          [2] => 9
        )
    )
)

I want to make all possible strings by using loop like as:

  a 4 7
  a 4 8
  a 4 9
  a 5 7
  a 5 8
  a 6 7
  a 6 8
  a 6 9
  b 4 7
    .
    . 
    .
  c 6 9

Array items can be change. Please suggest me optimal way for it.

I am getting for two layers of array, But it's dynamic array sometime item can be one or three.

CodePudding user response:

If I understand you correctly, you're looking for something like this:

https://gist.github.com/cecilemuller/4688876

<?php

$ar = [];
$ar[] = ['one',['a','b','c']];
$ar[] = ['two',[4,5,6]];
$ar[] = ['three',[7,8,9]];
//$ar[] = ['four',[10,11,12]];

function get_combinations(array $arrays): array {
    $result = [[]];
    foreach ($arrays as $property => $property_values) {
        $tmp = [];
        foreach ($result as $result_item) {
            foreach ($property_values as $property_value) {
                $result_item[$property] = $property_value;
                $tmp[] = $result_item;
            }
        }
        $result = $tmp;
    }
    return $result;
}

$out = get_combinations(array_column($ar,'1'));
$out = array_map(function($v){return implode(' ',$v);},$out);
echo implode("\n",$out);

Based on one of the solutions in the comments from over there that was written in Java, I have converted it over to PHP like so:

function get_combinations(array $arrays): array {
    $n = count($arrays);
    $indices = array_fill(0, $n, 0);
    $combinations = [];
    while(true) {

        // grab current combination for the indices.
        $combination = [];
        for($i = 0; $i < $n; $i  ) {
            $combination[] = $arrays[$i][$indices[$i]];
        }
        $combinations[] = $combination;

        // Find the rightmost array that has more
        // elements left after the current element
        // in that array
        $next = $n - 1;
        while($next >= 0 && 
                ($indices[$next]   1 >=
                count($arrays[$next]))
        ) {
            $next--;
        }

        // No such array is found so no more
        // combinations left
        if ($next < 0)
            break;

        // If found move to next element in that
        // array
        $indices[$next]  ;

        // For all arrays to the right of this
        // array current index again points to
        // first element
        for($i = $next   1; $i < $n; $i  ) {
            $indices[$i] = 0;
        }
    }
    return $combinations;
}
  • Related