3

I want to loop through 3 arrays to create 1 single array with all 3 values in them.

See below for example and outcome.

Input:

array(
    '0' => array(
        '0' => array('a'),
        '1' => array('b')

    ),


    '1' => array(
        '0' => array('c'),
        '1' => array('d'),
        '2' => array('e')

    ),


    '2' => array(
        '0' => array('f')
    ),

)

Outcome:

array(
    '0' => 'acf',
    '1' => 'adf',
    '2' => 'aef',
    '3' => 'bcf',
    '4' => 'bdf',
    '5' => 'bef'
)
Havelock
  • 6,913
  • 4
  • 34
  • 42
tony
  • 305
  • 5
  • 18

4 Answers4

1

How about this?

// $old_array = your original array
$new_array=array();

for ($i=0; $i<count($old_array[0]); $i++) {
    for ($j=0; $j<count($old_array[1]); $j++) {
         for ($k=0; $k<count($old_array[2]); $k++) {
              $new_array[]=$old_array[0][$i].$old_array[1][$j].$old_array[2][$k];
         }
    }
}

var_dump($new_array);

It returns:

array(6) { [0]=> string(3) "acf" [1]=> string(3) "adf" [2]=> string(3) "aef" [3]=> string(3) "bcf" [4]=> string(3) "bdf" [5]=> string(3) "bef" }
verjas
  • 1,793
  • 1
  • 15
  • 18
1

Funnily I had the same problem a couple of years ago, so here's the solution I then came up with.

public static function combineElementsSuccessive($arry) 
{
    $result = [];
    if (empty($arry) || !is_array($arry)) {
        return result;
    }

    self::concatAndPush('', $result, $arry, 0);
    return $result;
}

private static function concatAndPush($str, &$res_arry, $arry, $index) 
{
    foreach ($arry[$index] as $key => $val) {
        $mod_str = $str . $val;
        if (isset($arry[$index+1])) {
            self::concatAndPush($mod_str, $res_arry, $arry, $index+1);
        }
        else {
            $res_arry[] = $mod_str;
        }
    }
}

See it in action

Nevermind the static methods, I had to integrate them somehow in an application full of legacy code ;-)

Havelock
  • 6,913
  • 4
  • 34
  • 42
0

Convert your array as following numbers array and run the code

$numbers = array(
array("a", "b"),
array("c", "d", "e"),
array("f"),

);

$f_nb = $numbers['0'];
$s_nb = $numbers['1'];
$t_nb = $numbers['2'];
$final_array = array();

for($a = 0; $a<sizeof($f_nb); $a++) 
{
    for($b = 0; $b<sizeof($s_nb); $b++) 
    {
        for($c = 0; $c<sizeof($t_nb); $c++) 
        {
            $final_array[] = $f_nb["$a"] . $s_nb["$b"] . $t_nb["$c"];
        }
    }
}

print_r($final_array);
Santosh Jagtap
  • 995
  • 8
  • 17
0

Try this:

$array = array(
    '0' => array(
        '0' => array('a'),
        '1' => array('b')
    ),
    '1' => array(
        '0' => array('c'),
        '1' => array('d'),
        '2' => array('e')
    ),
    '2' => array(
        '0' => array('f')
    ),
);

$outcome = array();
foreach ($array['0'] as $key => $value1)
{
    foreach ($array['1'] as $value2)
    {
        foreach ($array['2'] as $value3)
        {
            $outcome[] = $value1[0].$value2[0].$value3[0];
        }
    }
}
print_r($outcome);
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41