0

i have this code with missing part and the output:

<?php
$months = array('four'=>'April', 'five'=>'May');
Missing#6;
Missing#7;
echo $months['nine']."<br>";
echo $months['ten'];
?>

The output:

**September

October**

and since it has two lines missing, I tried doing


<?php
$months = array('four'=>'April', 'five'=>'May');
$months[] = array('nine'=>'september');
$months = array('ten'=>'october');
echo $months['nine'], "\n";
echo $months['ten'];
?>

but in the compiler only print October and an error for nine:

PHP Notice: Undefined index: nine in 1784341836/source.php on line 5

1
  • You are missing the square brackets after $months on the line that assigns ten => October Commented Dec 14, 2021 at 18:40

1 Answer 1

2

You can add values with associated keys by accessing the array at that key and assigning the value.

<?php
$months = array('four'=>'April', 'five'=>'May');
$months['nine'] = 'September';
$months['ten'] = 'October';
echo $months['nine'], "\n";
echo $months['ten'];
?>

This will return:

September
October

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.