PHP Front To Back
Lección 4 de 22 · PHP Front To Back [Part 4] – Arrays · TI y software
PHP Front To Back [Part 4] – Arrays
you guys welcome to part 4 of PHP front-to-back now in this video I want to get to arrays and then maybe loops I'm not sure it was actually quite a bit
that we're going to go over with arrays and I want to touch on everything so what I might have to put loops in the next video all right but we'll see what happens so
we have our PHP sandbox folder people have been following along and we have the variables dot PHP file which we worked in in the last video now we're going to create a new file and we're
going to save that as a raid dot PHP okay and let's throw in our PHP tags and an array is basically a variable that can hold multiple values so variable that holds multiple values right and is
basically three types of arrays so first we have index we have associative and we have multi-dimensional sorry it's hard to talk and type at the same time so let's first go over index these are the
biggest the easiest type of array probably the most common so let's say we have a list of people that we want to put into an indexed array so what we would do is create a variable just like
we would with a regular variable and then we're going to set that to a special function called array and then we can put in our names okay let's say we have Kevin Jeremy and Sarah okay so
we have these three people that are stored in this one variable now if we want to access one of these let's say we want to echo let's settle people and let's say one so
let's save that and see what happens when we run it I'm going to go to localhost / PHP sandbox / arrays dot PHP when we get Jeremy now if you've never worked with the Rays before this might
confuse you might be asking why not Kevin he's he's the first one the reason for that is that arrays are zero based okay and and pretty much pretty sure every programming language there's zero
base they started zero and they go up so zero one two so with one we're getting Jeremy if we were to say people zero then we get Kevin okay so just try to remember that they're always zero based
now if we try to access people three which we know there isn't one zero one two and try to run that we get an undefined offset okay so that's not defined so let's go ahead and comment
that out and then we'll create another array and this one I want to create an array of numbers okay because you can do different data types these are obviously all strings numbers we could do without
having quotes around them alright so it was an array of numbers and we could say echo IDs say – and reload and we get twelve okay because that's the number two position now there's also another
way to create arrays so let's say we want an array of cars and you can do it without actually using the array function there you can use just brackets so if we go OK Honda
Toyota and Ford and this is equivalent to using the array function and just putting Honda Toyota Ford inside there we can act with it the same way so it's a cars one and sure you guys know we're
going to get we're going to get Toyota okay so it's up to you what way you want to define a race I usually use this way and actually not sure why I guess it's a little shorter but you can use either
way now if you want to add something to an array that's pretty easy as well so let's add a car so this is 0 1 2 so we know that the next one is going to be 3 so we could do cars cars 3 and put that
to let's say Chevy and then down here so they cars 3 will get Chevy now in some cases you're not going to know what index you're at but you're still going to want to add to that array so to do
that you could simply just do cars and then an empty set of brackets and I set that let's say I don't know BMW ok so now if we go when we do 4 and reload we get BMW ok so you can choose to use the
index if you don't know it you can just use some empty brackets and that'll add it to the last one now when it comes to arrays as though there's a whole bunch of different functions that you can use
I'm not going to go over all of them or even close to all of them because we haven't gone into functions yet but there are a couple that are very important to know so one of them is
count so let's say you want to know how many cars you have okay and that array so you can just use the count function and you can pass in cars so if we save that and we reload actually we need to
echo it and reload and we get 5 ok so it counts these 3 plus p2 um comment dado another one is print our and this is basically echo for a raise it'll actually show you the whole range
so let's throw in cars and reload and you can see it just prints out the entire array onto the screen so that's a very handy function for debugging when you want to see what's what exactly is
in that array now you may want to take it a step further and look at the data type that's in there as well for that you can use fog dump this is the very popular debugging function as well okay
so that's not in reload and it shows us that this type what this is is an array it has five values and it shows us each in each key and value and it also shows us the data type and the number of in
this case the number of characters okay so this is very handy this pretty much tells you everything you need to know about not only the array but all the variables inside of it or all the values
inside of it and var dump isn't just for arrays you can dump any variable and look at its data type and you know the number of characters so now that's that's pretty much it for indexed arrays
is not much more to look at so now we're going to look at associative arrays so an associative array allows you to define the key and the value with index basically the keys are the the number so
0 1 2 but with associative arrays we can assign that key to whatever we want and any data type we want so let's say we want an array of people so I'm going to say array just like we would normally
except we're going to define a key so let's say Brad please that is the key and then let's say the age we'll use as the value okay so you can see we're using the string as a key and we're
using a number as a value it could just as well be the other way around all right add a couple more here so say Jose and set him to 32 and let's set William to
37 all right now if we want to access let's say Brad's age we could do echo people and then we want to use brackets just like we did up above with indexed arrays
and we want to put in the key which is going to be Brad okay so the key is Brad and then it will print out the value which is 35 ok let's create another one called ID and we'll use the other syntax
which is you can just bracket and let's set IDs to two strings okay I'll set the idea of 22 to Brad on set 44 to Jose and 63 to William and then if we want to echo let's say we
want to find out the name of a certain ID then we'll do echo IDs and then let's say 20 to reload and we get Brad okay now if we want to add to an array an associative array we can do that as well
so let's do we want to add two people will say people and set a name here we'll say Jill I'll set her age to 42 so now if we echo out people and Jill it gives us 42 okay and we can just as
well use print are okay now you may be asking why you know we have people up here and here and why it's working for this one because we're just basically overwriting this people with this one
okay so that's how it works let's reload this and it shows it shows us the array with the key and the value all right and we can of course use var dump as well and there we go so those
are associative arrays not too difficult now the last type of array we're going to look at is multi-dimensional okay so basically what this is it's an array within an array so let's say we have a
variable cars we're going to set that to an array and then within that we're going to set other arrays so we'll go ray and let's put on des and let's say we also want to put the number of cars
that they have in stock and also the numbers that have been sold so we'll take 20 in stock 10 sold and there should be a comma okay and then we'll just copy that let's get fit in two more
times this will be Toyota let's say we have 30 and 20 and then this will be Ford and so we have 23 and we sold 12 now when we echo this out we're going to take ours and we're going to have to set
the bracket okay so the first one is going to be which one of these we want so let's say we want toyou 'te that's in the one position this is 0 1 2 and then which one of these we want let's say we
want to and that's going to be positioned zero okay so if we reload that we're going to get Toyota if we want to get let's say the number sold which is this that's
going to be still in the one position for this for the array and then zero one two okay so we want to for the second so it saved that in reload and we get twenty okay so that's all
multi-dimensional arrays work all right so we're going to go ahead and stop here we don't have the time to do loops in this video because I'm trying to keep these videos under you know 15 minutes
at the max so in the next video we're going to take a look at four loops while loops and also for each loops which we use to to loop through these arrays okay so we'll get to that in the next video
In this video we will look at PHP arrays. There are 3 types..
Indexed Arrays
Associative Arrays
Multi-Dimensional
CODE: Code For This Video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox2.zip
COURSES: Premium Eduonix Courses
http://www.traversymedia.com/eduonix-courses
SUPPORT THIS CHANNEL WITH A CUP OF COFFEE PER MONTH:
https://www.patreon.com/traversymedia
ONE TIME DONATIONS:
https://www.paypal.me/traversymedia