1

I would like to make an array of arrays with keys associated, and I don't really know what is the proper way to do it. For example if I have

    $array1 = array(
    "foo" => "bar",
    "bar" => "foo",
);
$array2 = array(
    "foo2" => "bar2",
    "bar2" => "foo2",
);

Would it be correct to write:

$key_array=array("first"=>$array1,
                  "second"=>$array2);

And, if not, how should I do this?

Thanks in advance

3
  • 3
    i think it would have been faster if you had tried and discovered that indeed it would work Commented Mar 26, 2013 at 15:15
  • 1
    Yes it's totally correct. Commented Mar 26, 2013 at 15:16
  • Yes, you are right, sorry. Thank you both Commented Mar 26, 2013 at 15:18

1 Answer 1

1

your code is correct, in fact you could also do:

$key_array = array(
               'first' => array('first' => $array1, 'second' => $array2),
               'second' => array('first' => $arra1, 'second' => $array2)
             );

and nest it as deep as you want.

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

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.