3

I want to create an array that contains multiple arrays. Each with a name. My code is pretty basic:

$main_array = array();

  $sub_array = array();

    $example1 = array("value1");
    $example2 = array("value2");
    $example3 = array("value3");

    array_push($sub_array,$example1);
    array_push($sub_array,$example2);
    array_push($sub_array,$example3);

  array_push($main_array,$sub_array);

This produces the following result:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => value1
                )

            [1] => Array
                (
                    [0] => value2
                )

            [2] => Array
                (
                    [0] => value3
                )

        )

)

I was hoping I could have something like this instead:

["main_array"] => Array
(
    ["sub_array"] => Array
        (
            ["example1"] => Array
                (
                    [0] => value1
                )

            ["example2"] => Array
                (
                    [0] => value2
                )

            ["example3"] => Array
                (
                    [0] => value3
                )

        )

)

Is that even possible?

4 Answers 4

5

try with this

$sub_array['example1'] = $example1;
$sub_array['example2'] = $example2;
$sub_array['example3'] = $example3;

instead of

array_push($sub_array,$example1); 
array_push($sub_array,$example2);
array_push($sub_array,$example3);
Sign up to request clarification or add additional context in comments.

1 Comment

This answer doesn't really address the "main_array" and "sub_array" part though.
3

Take a look at the PHP docs and associative arrays... http://php.net/manual/en/language.types.array.php

$arr = array(
    "main_array" => array(
        "sub_array" => array(
            "example1" => array("value1"),
            "example2" => array("value2"),
            "example3" => array("value3"),
        )
    )
);

Comments

2

Yes, you just need to set the elements key:

$main_array = array();

$sub_array = array();

$example1 = array("value1");
$example2 = array("value2");
$example3 = array("value3");

$sub_array['example1'] = $example1;
$sub_array['example2'] = $example2;
$sub_array['example3'] = $example3;


$main_array['sub_array'] =  $sub_array;

Though there is little benefit to creating all these seperate arrays when you can just create $main_array in one go:

$main_array = array(
    'sub_array' => array(
        'example1' => array("value1"),
        'example2' => array("value2"),
        'example3' => array("value3"),
    )
);

Also, since php 5.4 you can use short array syntax:

$main_array = [
    'sub_array' => [
        'example1' => ["value1"],
        'example2' => ["value2"],
        'example3' => ["value3"],
    ]
];

1 Comment

Thank you for such a detailed explanation Steve, especially with the time saving $main_array idea. The penny has finally dropped!
0

You can assign $example arrays into associated array:

$main_array = array();
$sub_array = array();
$example1 = array("value1");
$example2 = array("value2");
$example3 = array("value3");

$sub_array['example1'] = $example1;
$sub_array['example2'] = $example2;
$sub_array['example3'] = $example3;

$main_array['sub_array'] = $sub_array;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.