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'
)
4

4 Answers 4

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 ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

This one works the best as the array may be different in size. Thanks
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" }

Comments

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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.