CodeIgniter Forums
Turn 3 arrays into one multidimensional Array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Turn 3 arrays into one multidimensional Array (/thread-55452.html)



Turn 3 arrays into one multidimensional Array - El Forum - 10-26-2012

[eluser]Juan Velandia[/eluser]
Hello Everyone. Is there a CI way to turn this 3 arrays ...
Code:
Array
(
    [0] => a
    [1] => b
    [2] =>
    [3] =>
    [4] =>
    [5] =>
)

Array
(
    [0] =>
    [1] =>
    [2] => 123
    [3] => 456
    [4] =>
    [5] =>

)

Array
(
    [0] =>
    [1] =>
    [2] =>
    [3] =>
    [4] => 2012-12-12
    [5] => 2011-11-11

)
into one mulidimensional array:

Code:
Array
(
    [0] => Array
        (
            [arg_1] => a
            [arg_2] =>
            [arg_3] =>
        )

    [1] => Array
        (
            [arg_1] => b
            [arg_2] =>
            [arg_3] =>
        )

    [2] => Array
        (
            [arg_1] =>
            [arg_2] => 123
            [arg_3] =>
        )

    [3] => Array
        (
            [arg_1] =>
            [arg_2] => 456
            [arg_3] =>
        )

    [4] => Array
        (
            [arg_1] =>
            [arg_2] =>
            [arg_3] => 2012-12-12
        )

    [5] => Array
        (
            [arg_1] =>
            [arg_2] =>
            [arg_3] => 2011-11-11
        )

)

Any ideas? thanks in advance!




Turn 3 arrays into one multidimensional Array - El Forum - 10-26-2012

[eluser]CroNiX[/eluser]
assuming $arr1, $arr2, $arr3 are your 3 arrays you want to combine, and each one has an equal number of values:

Code:
$new_arr = array();

foreach($arr1 as $key => $value)
{
  $new_arr[$key] = array(
    'arg_1' => $value,
    'arg_2' => $arr2[$key],
    'arg_3' => $arr3[$key]
  );
}
haven't tested it


Turn 3 arrays into one multidimensional Array - El Forum - 10-28-2012

[eluser]Juan Velandia[/eluser]
Im going to give a try .. thanks a lot



Turn 3 arrays into one multidimensional Array - El Forum - 10-28-2012

[eluser]Juan Velandia[/eluser]
Cronix, It worked perfectly, thanks a lots (again)