– It’s often useful to store related values together in a single variable. To do so, you create an array, which is similar to a list. PHP has two types of arrays, indexed and associative. In this video we look at how indexed arrays work. Let’s indulge my passion for “Hitchhiker’s Guide to the Galaxy”, and create an array for some of the main characters. You store an array in an ordinary variable. So I’m going to call my array $characters.
Then, after the assignment operator, the traditional way to create an array is to use the array keyword, followed by a pair of parentheses. And the values that you want to store in the arraygo between the parentheses as a comma-separated list. The names of the characters are all strings, so they need to be in quotes. So, ‘Arthur Dent’, and then the comma needs to go outside of the quotes. Next we’ll have ‘Ford Prefect’, and we’ll finish with ‘Zaphod Beeblebrox’.
You can’t use echo with an array. Let’s see what happens if you try. If we save that and load this page into a browser, because I’m using PHP 7, I get this notice about array to string conversion. You can’t automatically convert an array to string in PHP. Then down here, it displays simply, Array. If you’re using PHP 5, all you will see is Array, you won’t see the notice.
So, we’ve stored our values inside an array, we need a way to get at those values. The way that you inspect the contents of an array is to use a function called print_r. So let’s go back to the editing program. I’ll comment out that line and then we’ll use print_r, and then a pair of parentheses after that. Between that you put the array. So, $characters, and if we save that,and refresh the page in the browser, this time we can see the contents of the array, and each array element has a number, starting from zero.
And this number is known as the array index. So, Arthur Dent has the index 0, Ford Prefect has the index 1, and Zaphod Beeblebrox has the index 2. And you use the array index to access the individual array elements. But before doing that, as I mentioned before, the array keyword is the traditional way to create an array in PHP. But ever since PHP 5.4, you can usethe same shorthand as in JavaScript.
So let’s just see that. I’m going to duplicate this line here, and then comment out the first one,and the shorthand doesn’t use the array keyword and parentheses, it gets rid of the keywordand just uses a pair of square brackets. That’s exactly the same as in JavaScript. So if we save that, we are still using print_r. If we refresh the page in the browser, we get exactly the same result.
So using the shorthand works exactly the same as using the traditional method with the array keyword. It doesn’t matter which you use, it’s very often just a matter of personal preference. I like to use the shorthand. You can add more elements to an existing array by using an empty pair of square brackets after the variable name, and assigning the value directly. So let’s just create a little bit of extra space down here, and then we’ll add another element to our $characters array, so it’s the array variable, followed by an empty pair of square brackets, the assignment operator, and the value you want to assign.
So we’ll add ‘Marvin’ to our list, and then another one, empty pair of square brackets, the assignment operator, and then let’s add the creator of the Norwegian fjords who was strangely reluctant to reveal his name. I wonder why. Slartibartfast. And if we save that and refresh this page in the browser, Those two elements have been added, and PHP has automatically assigned the next available numbers.
So Marvin has the array index 3, and Slartibartfast has the array index 4. And we can use that array index to access individual array elements. So let’s display the value of the second one, Ford Prefect. So go back to the editing program, comment out print_r, and then we will echoand it’s our $characters array, then we need a pair of square brackets and all we do is we put the index in there.
So we’re looking for the second element, that’s index one, not two, we’re counting from zero,so if we then display that, it gives us Ford Prefect. So that’s how you store multiple values in an indexed array, and access them using the array index. To create an indexed array, you could use the array keyword, with the values you want to store as a comma-separated listbetween a pair of parentheses. Alternatively, the shorthand syntax lists the values between square brackets, just like JavaScript.
PHP automatically assigns each value an index number, beginning from zero. You can inspect the contents of an array with print_r. And you access individual array elements using its index number between square brackets after the variable name. PHP has another type of array, called an associative array, and we’ll look at that next.
Question :
Why echo dont print array?
Why array display from print_r?