PHP Array Data Type - Indexed, Associative & Multi-Dimensional Arrays - Full PHP 8 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's say you needed a variable to store programming languages you might define each programming language as a separate variable like this for example and this is fine if you only have one or two programming languages but then it does not look or feel right because we're kind of repeating the variable name programming language and then appending the index to it one two three and then later if we get more programming languages then we're gonna have four five six and so on when you notice a repetition like that in your variable names it usually means that you need to structure your data in a different way there is a better way of doing this and it's called arrays arrays are just a list of values where those values can be of multiple data types so let's refactor this into an array and let's call the variable programming languages and we define an array by using square brackets so this is an empty array and then we could put any values in here so i'm going to put php comma java comma python now i put them in single quotes but you could also put them in double quotes it is up to you but i personally prefer single quotes unless i need to use a variable within the quotes there's also another way to define an array which is an older way and that is by using array keyword and then parentheses instead of the brackets so we could do it like this and this would work i prefer the bracket syntax though so that's what i'll be using throughout this course let's get rid of this and let's talk about how you would access each element in the array just like strings arrays by default are indexed by numbers starting at 0. so that means that this is at index 0 this is at index 1 and this is at index 2. as you may remember in the previous video about strings we were able to access individual characters using the 0 based index and square brackets so if we had something like name equals geo we were able to do echo name square bracket 1 and this would print out the second character in the string which is i same applies to arrays you're able to access individual elements using the zero based indexes so if you wanted to access the first element in the array you could do echo programming languages zero and this would print php it could access the second element and that would print java and so on note that unlike strings in arrays you can't access elements from the back of the array using negative numbers so going back to the string example we were able to do something like echo name -1 and this would give us the last character which is o now you cannot do the same thing with arrays if we try to do the same thing here we would get undefined array key warning and that's because a value does not exist at index negative one we only have index zero one and two so the number right here in the brackets is what we call index or a key you're able to define your own keys in arrays in php and we'll talk about that in a minute but if you don't define the keys php will automatically assign the numeric keys starting at zero you are basically able to treat arrays as different types of data structures like stacks queues collections hash tables and so on which makes arrays very powerful if you try to access a key that does not exist in array you will get a warning and you saw that before when we try to echo out at index negative one and if we try to do that again we get warning now same will happen if you try to access a index three for example because even though we have three elements it starts at zero so we have zero one two and the third key does not exist so this will result in the same error also note that that even though you get warning the value returned from accessing undefined key is actually no so if you var dump this or you assigned it to a variable you would get a null along with the warning to avoid such errors we can actually check if the item exists at that specific key or an index using eset functions so for example we could do var dump eset programming languages and let's do 0 and this will print true now if we did 3 this would return false if you don't exactly know that an item exists in an array at that index you could check first if it exists and then try to access it you can mutate the array and update the value at a specific index for example what if we wanted to change java to c plus for example we can do programming languages and then specify the index of java which is one and then set that to c plus plus and if we refresh it will print nothing because i forgot to echo that out so let's echo programming languages one and this will print c plus plus we can also use var dump or print r to see the contents of the array so we could do vardhan programming languages and this will print an entire array and this also includes the types so a less verbose way to do this is by using print r and let's remove this echo from here and this looks better if you want to get extra fancy and make it look even better you can surround it with html pre-tags and now it looks more readable to get the length of the array we can use function called count and we can do echo count and pass the array variable here and this will give you number of elements in that array so this should give us three let's remove c plus plus from here and just add c plus plus as a new element to the array we can do that by using square bracket syntax again but instead of assignment we do that before the assignments so programming languages square brackets and then put the value that you want to push to an array so we can do c plus plus here and now if we repeated the same thing right after let's add some break line here we refresh and now we have c plus plus as the last element and the count of array is four so this syntax here basically just pushes elements to the end of the array another way of pushing element to an array is using array underscore push an array push just mutates the array it means that the variable we pass here is passed by reference so anything array push does to that array will actually modify the original array which is here so we could pass that array as the first argument and then we could pass multiple arguments here to push multiple elements to array and let's remove that if we refresh we have c plus plus c and go right here as mentioned before you are able to define and name your keys the key has to be either a string or an integer when you have named keys in an array it's called an associative array so for example instead of doing var dom programming language is zero wouldn't it be nice if we could do something like vardon programming languages php which would give us some more information about that specific language and that is entirely possible in php because you're able to define your own keys so let's refactor the above array to contain custom keys so we're going to redefine that array called programming languages and we defined the key before the value so we said php and then equal sign and a greater than sign and then the value so we can set this to the current stable version of the language so this would be 8.0 and then python would be 3.9 let's print the array and it prints the array and these are the keys now so now you're able to access the array elements using those keys so programming languages php will print the version of the php and that is 8.0 if you try to access a key that does not exist you'll again get the warning we could also add other programming languages to this array by assigning the value to the specific key so let's add a programming language go with this version number so we could print this out and we get it right there this would of course work for indexed arrays as well where instead of specifying the string keys you would specify the numeric case another cool thing about arrays is that you could access elements by specifying the keys into variables so for example instead of hard coding this here you could put this into a variable called new language and then define new language as go and this will return the same result also as i mentioned before arrays can have values of multiple different data types it can have strings booleans integers floats and you can even have arrays as values which makes it possible to have multi-dimensional arrays let's refactor this code to have a bit more information than a version number because you could have multiple versions right so let's get rid of this and instead of setting php to a specific version number we can set it to another array and then this array can have its keys and values and now i'm just going to fill in these fields all right so let's print this on the screen and we get this detailed information now let me zoom out a bit and we see the creator extension website whether it's open source or not so as you notice here we have multiple data types we have strings we have booleans we have integers we have floats and we also have arrays itself so as you can see representing data in such form can be very powerful and even more powerful when you learn how to work with arrays using loops and built-in functions which will be covered in later videos accessing multi-dimensional arrays are actually pretty simple you just specify the key of the dimension you want to go into so for example if you want to access the extension or the website you just need to change the keys so you have php and then website so we would go here and we will do echo programming languages and php and then specify another key website and this will print the website right here same goes for the sub array here we could access the first version in the list and print its release date we could do echo php versions and then we could access the first element by using the zero based keys because we don't have the keys specified and php assigns the numeric indexes by default so they would be zero and then we could access the release date using the release date key and if we print that on the screen we get the correct date if you mess this up and you put a key that does not exist in any of these arrays so for example if you put three in here instead of zero you'll get warning and now you're getting two warnings because the first warning is telling you that array key three does not exist and then second warning is telling you that you're trying to access release date on something that is not an array because when you get this error right when you're getting undefined array uh error the value of it is actually no if we've already done this you'll see that the value of it is no if you have multiple keys that are the same the last one will overwrite the others so for example if we have array and then we do print r array we see that the element at index 1 is actually baz and not bar that's because baz had the same key as bar and it overrode also the keys have to be either strings or integers though php will try to cast the keys when possible so let's change this array a bit and what's going to happen here is that there will only be one value and it will equal to d and if we refresh the page we get that one value and the key is one that's because true gets cast into one one is one and overwrites a with b then string one overrides the integer 1 with c and then 1.8 gets cast to integer and when floats get cast to integer the decimal gets removed and this also equals to 1. and therefore it overwrites everything before and the value is just d that's why we get only a single element here also nulls as keys are cast into empty strings so for example if we set this to e and we refresh this is just an empty string and then we can access that the same way echo array empty string and this will print e or we can access it echo array null and it will print e so these keys in php are not required as you saw before you could have arrays without defining keys it even allows you to only have keys on some elements when there is no key php will automatically assign the integer key which is just an increment of the largest previous integer key and if there was no previous integer key then it starts at 0 which is what we see here is happening and that's the default behavior let's change these two letters and again the indexes are automatically assigned but if we were to define a key for one of them for example define 50 here and assign it now we have 0 1 50 and this is the biggest integer key so the next one will be 51 52 and so on and if you refresh the page that's exactly what's happening so it's something to be aware of when you're setting the numeric keys on only some of the elements of the array there are a few ways you could remove elements from arrays one way is by using a function called arraypop which basically just removes the last element from an array and returns it so if we did echo array pop array this would return e and remove it from the original array so if we refresh we get e and then if we print our array here we see that e is no longer there it could also remove the first element of the array by using a function called array shift which will remove the letter a from the array so if we refresh we get a and a is no longer in the array notice here that when we removed it from the beginning the array got re-indexed and we're going to talk about re-indexing next but before removing element we saw that a's index was zero so when we remove this you would think that value b would retain the index 1 but that's not what's happening when you use array shift it will re-index the array also note that there is no longer 50 as the key for the c because array got re-indexed and the key of the c is 1. it will only re-index the numeric key so if you had a non-numeric key here for example fu equals to e this would not get re-indexed so if you refresh this will remain same another way of removing element from an array is by using a function called onset and you already know what onset is because we use that to destroy variables but when used on array and you specify the index it will remove that element from the array if you don't specify the index it will destroy entire array so for example if we do onset array and then print our array right after we get undefined variable array because the variable gets destroyed if we specify the key here so let's specify 50 this will remove the element c from the array and if we refresh we no longer see the element c we could also remove multiple elements by specifying multiple arguments so you could do onset array 50 and then unset array 1 and this should remove c and b from the array if we refresh that works note that when you're removing elements from array using onset the array will not be re-indexed so for example if we were to remove just the second element in the array which is b the maximum integer on the key will be retained and it will now be re-indexed so if we do that and we do print r we say the first element is at zero the next one is at 50 then 51 52 and so on let me show you a better example so if we have one two and three as array elements and we remove all of them and we print the array now we have an empty array but if we were to push another element to an array using the square bracket syntax for example one you would think that this would get pushed at the zeroth index but it will actually get pushed at the third index in the array the maximum integer key is retained and the maximum integer key in this case is two and when we add another element to array the next integer key would be two plus one which is three and this is essentially same thing as setting the value to three and if we refresh we see that the key of the value one is three so let's talk about casting now let's delete all this and create a variable with some scalar value like five for example and then try to cast that to an array and this will actually turn this value into an array and this will become the first element in the array so if we refresh that we see that the value 5 is the first element if we change this to a string the same thing will happen if you're casting a null to array it will just create an empty array so if we refresh we have an empty array and before we wrap up this video i want to show you another way you could check if key exists in an array and that is by using a function called array key exists and you specify the key as the first argument and then pass the array as the second argument so let's define an array here called a equals one and b equals to null and let's do var dump array key exists a an array and this will print true because it exists and on the bottom let's also use eset and i will show you what the difference is let's do var dump and this is also true so the difference here is that array key exists will tell you if the key actually exists in the array or not while eset will tell you if the key exists and it's not null so in the case of the key b here the b's value is no if we were to check that with array key exist this would return true but if we were to check it with the eset it would actually return false because the value is no so if we refresh this we get true and we get false so it's just something to be aware of and be cautious when you're using eset but sometimes you may want to know if the key truly exists in the array whether it's null or not and in that case eset would not work and it may result in some mysterious bugs that are hard to find thank you so much for watching please give this video a thumbs up share and subscribe and i will see you on the next video where we'll talk about operators
Info
Channel: Program With Gio
Views: 7,215
Rating: undefined out of 5
Keywords: php arrays, php associative arrays, php indexed arrays, php, php8, learn php, php tutorial, php course, learn php the right way, beginner friendly php course, full php course, php in 2021, advanced php course, beginner to expert php
Id: C8ZFLq24g_A
Channel Id: undefined
Length: 16min 52sec (1012 seconds)
Published: Mon Dec 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.