2

I have the following code:

$tab=array(
    "january"=>array(300,420,530,400),
    "february"=>array(420,440,410,380),
    "march"=>array(330,310,250)
     );

How can I add another element to the $tab array called "april" with values 280 and 290?

2 Answers 2

2

Try this

$tab=array(
    "january"=>array(300,420,530,400),
    "february"=>array(420,440,410,380),
    "march"=>array(330,310,250)
     );
$tab["april"] = array(280,290);
Sign up to request clarification or add additional context in comments.

Comments

0

In PHP you can add to an associative array just by doing an assignment. For example:

$tab['april'] = array(280, 290);

Note that newer PHP versions support the concise [] shorthand notation instead of the array function so you can write:

$tab = ["january"=>[300, 420, 530, 400], 
"february"=>[420, 440, 410, 380],
"march"=>[330, 310, 250]
];
$tab["april"] = [280, 290];

2 Comments

Thank you very much. I have another question. How can i add a variable which calculates the average of the numbers of every month? Example: January = 412,50. Help would be much appreciated.
I suggest you post a separate question, and also, give it a try yourself, post your code, and how your code's output is different from what you desired. This site is not about writing the code for you, it's about helping you learn and find errors in the stuff you attempt!

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.