declare

46: Insert data into array in PHP - PHP tutorial



Sharing buttons:

today we're going to talk about how to

add data inside an array and this is

whether to raise MT over already has

data inside of it we're going to talk

about how to add data to existing arrays

so the first thing we need to do here is

we need to create an array so I'm gonna

go ahead and create a variable called

data I'm gonna set it equal to an array

do you notice that it 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 gonna use three different

methods in order to insert data into

this array and all these methods are

gonna work

whether you have an empty array when a

Raider already has data inside of it

it's basically just gonna add data

behind the array so if you have data

it's gonna be the last data goes behind

there so the first method we're going to

use is we're gonna go and go down to

next line we're gonna reference to our

variable data by writing variable data

and we're gonna add brackets behind it

instead of equal to some kind of data so

right now we can for example say we have

a string called Daniel that needs to be

inserted into this array and then

because we already created there rain

that's up here and then added the data

underneath here now if I were to echo

this out inside the browser of course

the echo variable data brackets and we

want to reference to the first data

because right now because there was no

data inside the array

Daniel is gonna be the first data which

is equal to 0 if I were to go back to

the browser so you guys can see it says

Daniel now in order to see everything

we're doing in here let's actually go

ahead and not echo out just the first

data let's go ahead and write something

called print underscore our parentheses

and then insert our variable data inside

of it because right now it's actually

gonna write out every single data inside

disarray up here so if I 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 if we're

gonna 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 guys can see we get a second piece

of data

so by adding these underneath your array

you're just gonna 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 I

were to go back to my code and delete

these two data go back to my browser

refresh it you guys can see we now lose

the data so we need to make sure we

already have the data inside the code

otherwise it's gonna loose it again now

the second way we can add the data to

this array is by going in and using a

PHP function called array push so array

underscore post parenthesis now the

thing about array push is that we need

to tell it which the raid we 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

gonna add a comment and then I need to

tell it which data I need to insert so

right now could also add Daniel save it

go back to my browser refresh and you

guys can see we now get Daniel again if

I want to add more data I can go ahead

and copy this paste it 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 code to

write because this is more code to write

than the previous method what we can do

is we can just go ahead and use this

array push and then keep adding data

behind the existing data we have in here

so if we were to say after Daniel comma

15 comma 23 for example save it refresh

the browser you guys can see we can also

do it this way which is kind of 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 up 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

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

arrays and this is something that's

quite useful for you to know if you want

to do something more complicated in PHP

like opticore into PHP or something so

hope you guys find us useful and I'll

see you guys next episode