Chevron Left Icon
Ordered Arrays
Lesson 1 of 2
Chevron Right Icon
  1. 1
    So far in our PHP programming, we’ve been thinking about individual pieces of data. We’ve seen how useful variables can be for holding a single value, for example. But as our programs grow more com…
  2. 2
    We can construct ordered arrays with a built-in PHP function: array(). The array() function returns an array. Each of the arguments with which …
  3. 3
    In addition to using array(), we can also create an array by wrapping comma-separated elements in square brackets ([ ]). This feature is sometimes referred to as short array syntax, and more clos…
  4. 4
    Since arrays are a more complicated data type than strings or integers, printing them is slightly more challenging. Using echo won’t have the desired result: $number_array = [0, 1, 2]; echo $numb…
  5. 5
    The individual elements in an array can be accessed using the array variable’s name, and the location index surrounded by square brackets ([]), for example: $my_array = [“tic”, “tac”, “toe”]; ech…
  6. 6
    We can make adjustments to existing arrays—we don’t have to create a new array when we want our array to change. We add elements to the end of an array by taking the variable name and appendin…
  7. 7
    In the previous exercise, we learned how to add single array elements and to change array elements at a given index. PHP also provides us with built-in methods for removing array elements, and for …
  8. 8
    We saw that array_pop() and array_push() deal exclusively with the end of the array (the index at the length of the array minus 1). PHP also provides functions for adding and removing elements from…
  9. 9
    We mentioned that arrays can hold elements of any type—this even includes other arrays! We can use chained operations to access and change elements within a nested array: $nested_arr = [[2, 4], [3…
  10. 10
    We covered a lot in this lesson! Great job. Take a second to review everything you learned: + Arrays are ordered collections of data that are a type of data structure fundamental to computer sci…
  1. 1
    Ordered arrays are awesome when we have data that lends itself to being collected into an ordered (indexed) list. But data can be collected and organized in lots of ways. Imagine we wanted a data…
  2. 2
    Associative arrays are collections of key=>value pairs. The key in an associative array must be either a string or an integer. The values held can be any type. We use the => operator to associ…
  3. 3
    As with ordered arrays, using echo to print an entire associative array is not very useful: $grades = [ “Jane” => 98, “Lily” => 74, “Dan” => 88, ]; echo $grades; // Prints: Array We …
  4. 4
    We access the value a given key points to using square brackets ([]): $my_array = [“panda”=>”very cute”, “lizard”=>”cute”, “cockroach”=>”not very cute”]; echo $my_array[“panda”]; // Prints: very c…
  5. 5
    The same syntax that adds new array elements can be used to change existing elements: $new_arr = [“first” => “I am first!”, “second” => “I am second!”]; $new_arr[“third”] = “I am third!”; echo $…
  6. 6
    Associative arrays can use integers as keys, in addition to strings. $num_array = [1000 => “one thousand”, 100 => “one hundred”, 600 => “six hundred”]; echo $num_array[1000]; // Prints: one thousa…
  7. 7
    PHP also lets us combine arrays. The union (+) operator takes two array operands and returns a new array with any unique keys from the second array appended to the first array. $my_array = [“pand…
  8. 8
    There are two ways to assign one variable to another: + By value—this creates two variables which hold copies of the same value but remain independent entities. + By reference—this creates two var…
  9. 9
    You learned so much in this lesson! Let’s review: + Associative arrays are data structures in which string or integer keys are associated with values. + We use the => operator to associate a…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo