Indexed Arrays5:13 with Alena Holligan
An array is a compound variable type meaning it can contain more than one value. We'll start with a basic indexed array, which can be used as a list of items: shopping list, days of the week, the top ten scores in a game, or a todo list.
Arrays in PHP are actually ordered maps. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.
See more about the Function for Creating Arrays
Shortcut for Assigning Values
Leave off the array keyword and use square brackets instead.
$learn = ['Conditionals', 'Arrays', 'Loops'];
[MUSIC] 0:00 An array is a compound variable type, 0:04 meaning it can contain more than one value. 0:07 An array can be a list of items such as a shopping list. 0:11 It's still one variable just like all your shopping items are in one list, but 0:15 it can hold multiple values. 0:20 Any time you want to store a list of items like the days of the week, 0:22 the top ten scores in a game or to do list arrays are a great choice. 0:26 Arrays are one of the most flexible and useful ways to store information and 0:32 PHP provides several different ways to create and use arrays including something 0:37 called an associative array, and a nested array called a multi-dimensional array. 0:41 But let's start with a basic array. 0:47 A simple list of values. 0:49 Let's create a new file named indexed arrays. 0:53 Open and close the PHP tags, and we're ready to get started. 1:02 We name our array variable just like any other variable, 1:07 a dollar sign followed by an underscore or letter and 1:11 then any combination of letters, numbers, or underscores. 1:14 Let's call this array learn and use the assignment operator. 1:18 Next we use the keyword array. 1:24 And then within parentheses, we add our list of values separated by commas. 1:27 Conditionals, arrays and loops. 1:33 To check out what our array looks like, we can use the ever useful var dump function. 1:41 And let's run our script. 1:49 php indexed_arrays.php. 1:52 This tells us quite a bit of information about the array. 1:59 First off, we see that the size equals 3, 2:02 because there are 3 items, or elements, in the array. 2:05 Then we see a separate line for each element. 2:10 First we see the numbers one through two followed by equal greater than. 2:14 The equal or greater than sequence is referred to as a double arrow. 2:20 This double arrow represents the relationship between the key and 2:25 its value. 2:29 Each array element requires a key. 2:31 PHP automatically assigns a numerical key by 2:34 incrementing the largest previously used integer key. 2:38 By default, arrays are zero indexed, 2:42 meaning the first value is zero then one then two and so on. 2:46 You'll see in a later video that you can specify your own key and 2:52 they don't always have to be numeric. 2:56 The var dump shows us that our first element has a key of zero and 2:58 then describes the value. 3:03 The value is of the type string, the string it contains is conditionals. 3:05 And the length of characters is 18, 3:12 each additional element is described on its own line. 3:15 If I want to access the second value Arrays, I would use its key. We echo, 3:20 the variable name, learn, followed by the key within square brackets. 3:28 For the second element, I would use the key of one. 3:35 Let's run the script again. 3:39 Now we see after our var dump, that we display the value arrays. 3:41 We've looked at the array using the var dump function, 3:47 as well as accessed a single value by specifying a key. 3:50 I want to point out that you can't just display the array directly. 3:55 Let's test that out. 3:59 echo learn. 4:01 Now when I run my script, I see an error. 4:04 There's a notice about an array to string conversion. 4:08 This is because arrays store more complex data and 4:13 PHP doesn't know how to format that data. 4:16 So if we want to display the array values we have to tell PHP 4:19 how we want them formatted. 4:24 PHP gives us a function called, implode, 4:26 that allows us to join array elements together into a single string. 4:28 We can then separate the items in this new string 4:34 using another string of our choosing. 4:37 This separator string can be anything we want, even multiple HTML tags. 4:40 Let's comment out these display lines. 4:46 Then we’ll add echo Implode and 4:54 our separator which will be a newline character. 4:59 Then we pass in the array learn. 5:02 Now let's run our script. 5:07 We see a list of array element values. 5:09
You need to sign up for Treehouse in order to download course files.Sign up