FAQ: Associative Arrays - Numerical Keys

This community-built FAQ covers the “Numerical Keys” exercise from the lesson “Associative Arrays”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Numerical Keys

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the third code snippet on Numerical Keys under Associative Arrays, why does echo $num_array[1001] print New Element in $num_array, since there’s no previous key 1001 previously declared i don’t see how it’s possible.

The third snippet is:

$num_array = [1000 => "one thousand", 100 => "one hundred", 600 => "six hundred"];
$num_array[] = "New Element in \$num_array";
echo $num_array[1001]; // Prints: New Element in $num_array

$num_array is an associative array with integer keys (instead of string keys).
The largest key used is 1000. Then, we append a string to the array. We haven’t explicitly specified any integer or string key for this new element. But, there should be some way to access this element. If there is no key specified for an element, we should still be able to access the element by its index.
The largest integer key used in the array was 1000. The new element’s key or index will be one more than the largest integer used. So when we append the new string element “New Element in $num_array”, the element is assigned the index (or integer key) of 1001.