Insert Data Into Array in PHP

today we're going to talk about how to add data inside an array and this is whether the race mt over already has data inside of it we're going to talk about how to add data to existing a race so the first thing we need to do here is we need to create an array so i'm going to go to create a variable called data going to set it equal to an array you notice that i didn't put any kind of data inside the array because right now what we have here is an empty array so there's no data inside of it now we're going to use three different methods in order to insert data into disarray and all these methods are going to work whether you have an empty array on a radar already has data inside of it is basically just gonna add data behind array so if you have data is going to be the last data goes behind her so the first method we're going to use is we're going to go to go down to next line going to reference to our variable data by writing variable data I'm going to add brackets behind it and set it equal to some kind of data so right now we can for example say we have a string called Daniel needs to be inserted into this array and then because we already created the rain that's up here and then added the data underneath here now for what equities out inside the browser was the echo variable data brackets we want to reference to the first data because right now because there was no data inside the ray Daniels gonna be the first data which is equal to 0 if i were to go back to the brow so you guys can see is this daniel now in order to see everything we're doing in here let's actually go ahead and not a clout just the first data let's go ahead and write something called Prince underscore our parentheses and then insert our variable data inside of it because right now it's actually going to write out every single data inside disarray up here so for were to go back to the website you guys can see we have array and then we have the first value which is equal to Daniel now for what's going to need Daniel and then copy paste it one more time add another piece of data which could be 15 in this example just a number go back to the browser refresh you get some sleep you get a second piece of data so by adding these underneath your array you're just going to go ahead and add data to it now this is the first method we can use and this is not one that i usually use because it's it takes a bit more code to write it this way now if we were to go back to my code and delete these two data go back to my browser refresh said you guys can see now lose the data we need to make sure we already have the data inside the code otherwise it's going to do second now the second we can add the data to this array is by going in and using a PHP function called her a push so ray on the scope horse parentheses now the thing about our apush is that we need to tell it which the red you want to push data inside so we have one up here called variable data so I'm gonna put that in as my first parameter then I'm going to add a comma and then I need to tell which data I need to insert so right now i could also add Daniel save it go back to my browser refresh and you guys can see when i get Daniel again if i want to have more data i can go ahead and copy this paste underneath here and again i can add for example a string like 15 and now we have a second piece of data now if this is too much co2 right because this is more code to write then the previous method what we can do is we can just go ahead and use a red push and then keep adding data behind the existing data we have in here so we say after Daniel comma 15 comma 23 for example save it refers to browser you guys can see we can also do this way which is kinda nice so by using a rate push we can avoid having to write more code then by using the one we just add the bracket and set it equal to something so this is how we can insert data inside an array now just to show you guys if we were to go inside my variable data equal to an empty array and insert some kind of data inside of here let's call this one first then i might want to add another piece of data called second and now go ahead and refresh the browser you'll notice that we have first second then Daniel 15 and 23 like so because we just add the data behind the existing array so this is how you can add data inside erase and this is something that's quite useful for you to know if you want to do something more complicated PHP like object oriented PHP or something so hope you guys find this useful and i'll see you guys next episode