‫Guys.
‫So now we're going to look at looping through arrays.
‫And I'm going to show you a few different ways we can do it.
‫And we're also going to loop through just a regular array like this.
‫And an associative array like this okay.
‫And I just put this in here so we don't have to type it out.
‫We already know how to create arrays.
‫So we're going to do this down in the template.
‫And you can see I've created just an H three for each section for each thing I want to do.
‫And then just a UL because we're going to output list items in our loop.
‫So first off let's look at just looping through a regular array like this with just a bunch of strings
‫using a for loop.
‫So we'll come down here and go into this UL.
‫And basically I want to output each of these names right here in this names array as in a list item.
‫So let's start off by saying PHP four.
‫And I'm going to say as long as I is equal to zero.
‫And then we're going to say I.
‫Is less than now when you're using a for loop to loop through an array.
‫What you're going to do for the condition is put the the number of array items, because you only want
‫to loop that amount of times, right?
‫If you have five strings in an array, you want it to loop five times.
‫So what you can do is use the count function.
‫Remember that counts the length of the array.
‫So we're going to say count names.
‫As long as I is less than whatever that is.
‫And then we'll say I plus plus because we want to increment by one.
‫All right.
‫And then let's create our app.
‫And for.
‫And then what I want to do is within that I want to create a list item.
‫And then I'm going to say PHP.
‫Actually, we don't have to do echo.
‫We can just do our equals sign and let's say names and then whatever that index is of I.
‫Because remember we we access array values with the name of the array and then the index.
‫So if I do like names one for instance that's going to give me this one right here.
‫That's the one index.
‫So what's going to happen is that I is going to be wherever we start from zero and then up to the length
‫of the array, which in this case is what five.
‫So we want to put I for that index and then close up the PHP tag.
‫Let's reload.
‫And there we go.
‫So we have John Doe.
‫We have all the names being spit out through the for loop.
‫Okay.
‫So you can do it that way with a for loop, but for each is a much better way to do it.
‫That's what you're going to see mostly when you're dealing with arrays.
‫So let's do that next using the same names array.
‫So we have a ul I'm going to say PHP.
‫Now instead of instead of using a for I'm going to say for each.
‫And this is much easier in a few ways.
‫One all I have to do is put in the name of the array which is names, and then as whatever I want to
‫call each item in the array, which is a name.
‫So I'll do name singular right.
‫And then down here we're going to say PHP for n for each.
‫Okay.
‫So n for each.
‫And then in here I'm going to put a list item.
‫And I want to echo out the name.
‫Now I don't have to use an index like I do with a for loop.
‫All I have to do is use whatever I call this, which in this case is name.
‫All right, so I'll save that reload and I get the same thing.
‫So you can just take a look at both of these and see that for each is much, much cleaner.
‫You don't have to do all this.
‫You don't have to have three arguments.
‫You don't have to use the index.
‫All you have to do with A for each is say whatever the array is, whatever the item, and use that item
‫in here.
‫And this can be anything.
‫It doesn't have to be the singular version of this.
‫Like I could call I could call this hello.
‫And then if I change this.
‫To hello and reload.
‫That's going to work.
‫Okay, so you call that whatever you want just represents a single item in the array.
‫So now what I want to do is use a for each loop with the index.
‫I just want to show you that you can get the number index if you want.
‫So I'm just going to copy what we did here and go in this ul paste that in.
‫And all we have to do to get the index is say names as name, I'm sorry name as index.
‫And then use an arrow and then name.
‫Sorry.
‫That should have a.
‫Money sign.
‫And then you can use that index from within here.
‫Like let's say we want to.
‫Put the index before the name.
‫We'll say index.
‫I guess.
‫Yeah.
‫We'll just put string here.
‫Like that and then reload.
‫And now you see we get the index zero, one, two, three and so on.
‫So for whatever reason.
‫Now, the next thing I want to do is use a for each loop with an associate of array.
‫So our associative array is this right here users where we have key value pairs of name and email.
‫So let's go down here into this UL.
‫And I'm going to yeah we'll just go ahead and let's say PHP for each.
‫And then we want a PHP end for each.
‫And what I want to put in here is the users array.
‫So I'm going to say users as user.
‫So then I can use that user variable in here to get the individual users.
‫So let's do a list item and let's output.
‫We'll say user.
‫Now remember each of these is a user that has a name and an email field.
‫So I can say user name.
‫And then I'll just what do we want to do here.
‫We'll just concatenate.
‫Space, I guess.
‫And then we'll put the.
‫User email.
‫Close that up.
‫Actually, we'll put a colon like that.
‫So if I reload, you can see what I get is the user name colon and then the user email.
‫All right.
‫And then the last thing I want to do is show you how we can get the key names and values from an associative
‫array.
‫And we can do that using a nested loop, a nested for each loop.
‫So let's do actually I'll just.
‫Let's grab that, we'll get rid of this.
‫And then we're going to open up another for each loop here.
‫So let's say.
‫For each.
‫And what I'm going to put in here is going to be the user.
‫So user as and then we can get the key.
‫And the value.
‫So this is if you need the key, which in this case is name and email.
‫And then let's close that up.
‫So we want to end.
‫And for each.
‫And then what I'll do is we'll have a list item and we'll do.
‫Um, we'll just have the key.
‫Then we'll concatenate.
‫Put a colon space and then we'll have the value.
‫Oops.
‫We need to end that.
‫All right.
‫So let's reload.
‫And now you can see name John email John at email name Jane email and so on.
‫And in my experience, you probably won't be doing this that much because you really don't need the
‫key.
‫The value is really what you're focused on, but you'll be doing this all the time, getting data from
‫a database.
‫And then that data comes in the form of an array.
‫And then you loop through it and you put it out, put it into your website or into your user interface.
‫All right.
‫So we're going to take this a step further.
‫And in the next video create a little job listing page.