Combining array elements

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

See below example and result.

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

)

Result:

array (
    '0' => 'acf',
    '1' => 'adf',
    '2' => 'aef',
    '3' => 'bcf',
    '4' => 'bdf',
    '5' => 'bef'
)
+3


source to share


4 answers


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

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

Never mind static methods, I need to somehow integrate them into an application full of legacy code; -)

+1


source


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" }

      

+1


source


Convert the array as an array of the following numbers 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);

      

0


source


Try the following:

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

      

0


source







All Articles