0

I am trying to add keys to each of these two arrays so that I can access all the data as one array. Here is what I have. Tried multiple posts on Stack Overflow but so far nothing has concreted this concept for me:

$result = array(); 
$age_men = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$age_women = array("Sally"=>"32", "Debra"=>"30", "Miranda"=>"40");

$result += ["Men" => $age_men];
$result += ["Women" => $age_women];

print_r($result);

//desired output
Array
(
    [Men] => Array
        (
            [Peter] => 35
            [Ben] => 37
            [Joe] => 43         
        )

    [Women] => Array
        (
            [Sally] => 32
            [Debra] => 30
            [Miranda] => 40

        )
)   
3
  • $result["Men"] = $age_men; $result["Women"] = $age_women; Commented May 19, 2017 at 11:23
  • And what is the problem? Your code gives you exactly what you want: 3v4l.org/5U4Ok Commented May 19, 2017 at 11:25
  • your code also working then what is the problem ? Commented May 19, 2017 at 11:28

2 Answers 2

1

your code is working and another way is

$result["Men"] = $age_men; $result["Women"] = $age_women;
Sign up to request clarification or add additional context in comments.

Comments

0

Try like below:

$result["Men"]  = $age_men;
$result["Women"]= $age_women;

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.