Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Indexed Arrays 5:13
- Adding Array Elements 5:08
- List Quiz 5 questions
- Removing Array Elements 4:50
- Editing Array Elements 1:36
- Modifying Arrays 3 objectives
- Associative Arrays 3:48
- Associative Quiz 5 questions
- Mixing Data Types in Arrays 6:07
- Multidimensional Arrays 6:14
- Multidimensional Arrays 3 objectives
- Sorting Arrays 6:16
- Mixing and Sorting 5 questions
Well done!
You have completed PHP Arrays and Control Structures!

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
You can assign your own keys to array elements -- and they don't have to be numbers. In fact, your code can be easier to read and understand if you use a name for a key. This is called an Associative Array, because a specific key is associated with a specific value.
Arrays are also referred to as a hash or dictionary
Comma After Elements
The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.
Extract
You can use the extract function to extract the key value pairs as individual variables.
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
Gives us the variables $color = "blue", $size = "medium", and $shape = "sphere".
Example
You can even use spaces to line up the values.
$iceCream = array(
'Alena' => 'Black Cherry',
'Dave' => 'Cookies and Cream',
'Treasure' => 'Chocolate',
'Rialla' => 'Strawberry'
);
Shortcut for assigning values
$iceCream = [
'Alena' => 'Black Cherry',
'Dave' => 'Cookies and Cream',
'Treasure' => 'Chocolate',
'Rialla' => 'Strawberry'
];
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Arikaturika Tumojenko
8,897 Points1 Answer
-
Daniel Breen
14,943 PointsHelp understand the order of an array with both associative and non-associative behavior
Posted by Daniel BreenDaniel Breen
14,943 Points2 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up