Key Concepts

Review core concepts you need to learn to master this subject

PHP Apending Arrays

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, elements can be added to the end of an array by attaching square brackets ([]) at the end of the array’s name, followed by typing the assignment operator (=), and then finally by typing the element to be added to the array.

PHP Ordered Arrays

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, an ordered array is a data structure representing a list of ordered, stored data. The location of an element in the array is known as its index. The elements in an ordered array are arranged in ascending numerical order starting with zero.

PHP array funtion

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, an ordered array can be constructed with the built-in PHP function: array(). The array() function returns an array. Each of the arguments with which the function was invoked becomes an element in the array (in the order they were passed in).

Pass in arguments by including them, comma-separated, between the parentheses.

PHP Nested Arrays

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, nested arrays are arrays that contain other arrays as elements. Chained operations can be used to access and/or change elements within a nested array.

PHP print_r function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The built-in print_r() function outputs arrays in a human readable format.

To use the function, place an array or a variable with an array as its value in between the parentheses.

PHP array_push function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The PHP built-in array_push() function takes an array as its first argument. The arguments that follow are elements to be added to the array.

The function adds each of the elements to the end of the array and returns the new number of elements in the array.

To use the function place an array or a variable with an array as its value in between the parentheses.

PHP Count Function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The built-in PHP count() function takes an array as its argument and returns the number of elements in that array.

To use the function place an array or a variable with an array as its value in between the parentheses.

PHP Square Bracket Arrays

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, arrays can also be constructed using short array syntax. To use this syntax, simply wrap comma-separated elements in a set of square brackets.

PHP array_pop function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The PHP built-in array_pop() function takes an array as its argument. It permanently removes the last element of an array and returns the removed element.

To use the function place an array or a variable with an array as its value in between the parentheses.

PHP unshift function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The PHP built-in array_unshift() function takes an array as its first argument. The arguments that follow are elements to be added to the array.

The function adds each of the elements to the beginning of the array and returns the new number of elements in the array.

To use the function, place an array or a variable with an array as its value in between the parentheses.

PHP implode function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The built-in implode() function makes a string from an array. The function takes two arguments: a string to place between each element of an array and the array to be joined together.

The function returns a single string with the string argument, known as the $glue, inserted between each element in the array, known as the $pieces.

PHP Accessing Array Values

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, the individual elements in an array can be accessed using the array variable’s name, and the location index surrounded by square brackets [].

Remember that in PHP the location index starts at zero.

PHP array_shift function

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

The PHP built-in array_shift() function takes an array as its argument, permanently removes the first element from the array, and returns the removed element. All the elements in the array will be shifted to an index one smaller than their previous index.

To use the function, place an array or a variable with an array as its value in between the parentheses.

Reassign Array Element

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, individual elements in an array can easily be reassigned. To do this, we access the location of an element in the array using square brackets ([]), and then assign a new value using the assignment operator (=).

Accessing and Adding Elements

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, we can access the value that a given key points to using square brackets ([]) and the key. To add new elements to an associative array, we can use the assignment operator (=). We can also change existing elements using the same syntax that adds new array elements.

Assign by Value or by Reference

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

PHP arrays can be assigned or passed by value or by reference. PHP arrays that are passed by reference use the reference sigil (&). Here are some of the the important differences between the two:

  • Passed by value: this creates two variables which hold copies of the same value but remain independent entities.

  • Passed by reference: this creates two variable names (aliases) which point to the same space in memory. They cannot be modified separately!

PHP Associative Array

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, associative arrays are map-like structures, where keys are associated with values. When we need to access a specific value, we can use the associated key to find it.

In a PHP ordered array, the index locations are the keys. However, the PHP array type also allows us to assign meaningful keys to values. These data structures are called associative arrays.

For example, in the following associative array table, the key "num_legs" points to the value 4, and the key "material" points to the value "wood":

$table = ["num_legs" => 4, "material" => "wood"];

Joining Arrays in PHP

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, 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.

Removing Elements in Associative Array

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

A key=>value pair in a PHP associative array can be removed entirely using the PHP unset() function.

If the key used does not exist in the array, then nothing happens.

Numerical Keys in Associative Arrays

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

Associative arrays can use integers or strings as keys.

In PHP, ordered arrays are just arrays in which integer keys have been assigned to the values automatically. For example, the first element is associated with key 0, the second with 1, etc. Even though ordered arrays are the same structure as associative arrays, it’s recommended that you treat the two separately.

When adding an element to an array without specifying a key, PHP associates it with the “next” key. If no integer keys have been used, it will associate it with the key 0, otherwise it will associate it one more than the largest integer used so far.

Create Associative Array

$string_array = ["first element", "second element"]; $string_array[] = "third element"; // $string_array is now: ["first element", "second element", "third element"]

In PHP, associative arrays are collections of key => value pairs, where the key must be either a string or an integer and the value can be of any type.

The => operator is used to associate a key with its value. For example:

$grades = ["John" => 91, "Sally" => 94];

The associative array grades is defined using short array syntax. The key "John" points to the value 91, and the key "Sally" points to the value 94.

We can also define this associative array using array(). For example:

$grades = array( "John" => 91, "Sally" => 94 );
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