Learn PHP Arrays
Learn about PHP ordered and associative arrays and how this data type is used to store, access and manipulate data.
StartKey Concepts
Review core concepts you need to learn to master this subject
PHP Apending Arrays
PHP Ordered Arrays
PHP array funtion
PHP Nested Arrays
PHP print_r function
PHP array_push function
PHP Count Function
PHP Square Bracket Arrays
PHP Apending Arrays
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.
Ordered Arrays
Lesson 1 of 2
- 1So 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…
- 2We can construct ordered arrays with a built-in PHP function: array(). The array() function returns an array. Each of the arguments with which …
- 3In 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…
- 4Since 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…
- 5The 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…
- 6We 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…
- 7In 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 …
- 8We 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…
- 9We 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory