Arrays & Array functions | PHP for Beginners - Part 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] okay so here I have opened arrays PHP and we need to do the same thing in browser so here it is and this is the plain what we're going to do so we're going to go through all of this commented code and fill it with a with the actual code okay arrays in PHP are just like areas in other programming languages they are just variable which can contain series of values okay okay let's go ahead and create an array of fruits okay and I'm gonna give here like apple or let's line starts with banana then we have Apple and we have orange okay and let's print the whole array okay so I'm gonna use for dump to print the whole array of fruits and this will be the result so I see it in an ugly way so it printed actually everything it tells that it's an array of three elements the first one is string six banana second one is string five Apple 31 and six string six orange but it's not really nice to look at this so it prints nicely in the page source it has all like whitespace is there but HTML like just doesn't hear about white spaces so generally what I do I print pre tags in front of and before in front of and after the fordham statement and this is now how my code looks like that's that's nice and I also have like shortcut live to play it in my PHP store which prints this for me so next time when I'm printing some value of the array I will use that shortcut so don't like don't get confused okay so here are fruits and this is the result okay now let's print the first element of the full array like echo fruits its 0 for 0 in Co T's banana so we can access the same element in the same way the elements of apple and orange in PHP array start with index 0 this means that a Orange has index 2 and if we try to access index 3 elements of the array we don't have it and this let throws that undefined offset okay so let's leave it we'll leave this in to 0 and let's see how we can set elements ok I want to set a new element in the in the fruits array on on position like composition 0 for example and let's call it a peach okay and after this if I try to print the whole fruits array I will get the first one is peach then we have apple and orange ok so if I want to check if the area has element at index 2 or not so before before printing it so I can check it with either set function okay so let's let's print it here's a set fruits eighty index two which obviously should return true because we have element at index at index 2 and this orange however if we pass here 3 this returns false because we don't have any element at position 3 ok so let's see how we can append elements in the array thoughts comment on this code to avoid any kind of confusion and let's start writing fruits and then we write square as square brackets and and that's it so we can append right here and like peach for example okay so we have initial banana plant origin we're appending then peach right here and let's dump fruits and we see now we have four elements banana apple orange and peach the one thing I haven't mentioned when declaring an array is that this is the one way of declaring an array inside square brackets okay so the square brackets is just another way the like standard way to declare an array but we have another way and we can do it with air a word hey ray banana apple and orange and then they closing parentheses that's kind of old way and I generally don't do this but this still work and probably will work for a long time in PHP so I generally declare areas with square brackets just in case if you made that arrey arrey word declaration syntax don't get confused but because that still works really well okay so we we see how we can append any element in the array and let's print now the whole length of the array okay let's print echo count of fruits and he OC 4 so if I want to eight elements of the at the end of the arrow so this is this is one way to do this but we have another way special function for this and it's called terror a push okay array underscore push we give the first argument array and the second argument is the element I want to add in the in the array okay and we can add one more pitch at the end of this array and save this and have a look and we don't print of the Bay Area obviously so let's print fruits and here we see peach two times okay if I want to remove element from the end of the array I need to use array underscore pop we give fruits array and this removes that element and actually returns that removed element but it also changes the fruits arrays the length so it actually modifies the area okay so let's dump fruits once again and here we see the second page is not presented anymore okay so if we want to add an element at the beginning of the area we need to use array and shift we give the fruits array and we give the element like a pole for example I want to add a pole at the beginning also if I want to remove an element from the beginning I have for this area underscore shift okay we'll give the fruitarian that removes that added April okay so if I if I have like a string and I want to convert that string into an airy I have special function for this and let me copy this code and show this to you to be quicker okay so banana apple and pH they are comma separated string values and I'm using explode and that explode splits the string with the given even separator okay now this explode splits this string with comma okay and that returns the it's that array let's comment all other volume statements for now to have a look and this is the result of that for them if I have like every elements and I want to bake combine them into into a string I have function employed for this okay and if I want to implode my my fruits array into a string I can employ them I can give the glue the comma for example I can give or ampersand and I'm going to give fruits and let's print paste okay thought we are and here we see the concatenated fruits array so I have another function I have another function to print the whole area and that's four areas exactly that's called print underscore R and if we give fruits right here that simply prints the whole array but the same thing happens right here so the white spaces are just not displayed so we need to put this in a pre tag or just have a look in the page source and this is how the array is printed okay so that's I think nicer when we are working with the array so we don't don't have that extra lines okay so it's just for your information I'm used to were dumped and I'm always using it so I'm going to use it also right here so if I want to check if one particular element exists in array or not I have function for these array excuse me in underscore area we give this search element and let's say that I search Apple and I am curious if the Apple is in the in the fruits array okay and this that will be evaluated into true or false and obviously this will return true because my the Apple is definitely inside the efforts array if I just want to search the element index in the array I have to use a reenter score search and I give the Apple again and I give the fruits array and that will return the index of the Apple in the fruits array and we can print this and have a look and it returns one because Apple exists not this one but let's print right here the initial fruits array and we see Apple exists on position 1 inside it Eric okay if I have like several areas and I want to merge them together I have let me copy this array I have functions for this ok let me down please and I'm going to use array underscore merge and I'm gonna give fruits and vegtables for example and that finally will give me a single array and here is the array okay it merged however in PHP 7.4 we have version to merge the errors in an easier way using spread operator okay I'm gonna use far down also and create an erring and use the spread operator for fruits and spread operator for vegetables okay if your phpstorm doesn't really understand what is what's going on right here you just need to change your PHP language level into PHP 7.4 so I click this it changes and now it can understand what's going on okay and I can print this and the result is absolutely the same okay it's worked it works in the same way okay let's talk about how we can sort elements of the array okay we have a function sort as simple as this so we give fruits and that gives us the sorted version and that modifies the area it doesn't return new AIRAID modifies the fruits areand after this if I just fruits I see that fruits are now sorted first one is Apple second one is banana then we have orange and peach I have another functions you know like our sword which means reverse sort and that will sort in reverse order and I have you sort which basically accepts a call back okay some computer phone so we can give some computer function right here and compare our elements in just in the way we want okay so let's talk about filter map and reduce which exists in PHP also okay so we have array let me let me copy this code also and explain it okay let's pay true so I have numbers right here and I want to like filter numbers and take only events okay I use a rate filter function the first argument is the numbers array the next argument is the callback function and the callback function takes each individual number and every I check in the occultic function if the number is even then return true otherwise return false and that finally gives the even numbers and here's the result we have two four six and eight in PHP seven point four we have arrow functions we can replace that function with just FN and put an arrow right here and we don't even need that curly braces okay in fact this is the code what we need okay right here I will place it and that does the same thing and that's much easier to write and like easy to understand also and the result is the same okay let's talk about a remap okay so let's use the same same numbers and let's say that I want to make each number into its square okay for this I will use here a underscore map I give here call back in I'm going to use arrow function FN let's give FN that's the first argument of the event we will accept n right here and we will just return n multiplied on in and the second argument will be the numbers okay so that's the difference from any filter arrow filter first accepts numbers in then call back hairy map first accepts call back and then number okay that's why I'm using phpstorm I can click control and mouse and follow this and see what is the first argument what is the second one and I can do the same thing for every map okay in that file finally will return like a squares new array and we if we don't that squares a hair typo squares okay that will give me the new squares array okay that's that's cool and let's use array underscore reduced function to reduce the number of elements into single one and in this case let's calculate just the sum of the elements like FN they carry normally I will explain these things item and let's use carry carry plus item okay what this does is that it iterates over the numbers takes each item and passes that F and callback and also it passes that carry okay and that carry is like an accumulator so it collects the results of that one okay so this area reduces I like a high order function so you need you just need to get be familiar with how high order functions work in other programming languages like in JavaScript so if you don't have any idea how these works like reduce or every maple filter just I have video on JavaScript for these so you can just have an idea how they work and simply then you will be like you you will easily understand how you can interpret in Beijing PHP but that works fine and that returns the sum of the all elements of the numbers and let's have a look at the result and here we see it's 36 okay so here I have linked about all functions which exists regarding arrays and again check them out don't try to remember them and I will put the link in the video description okay there are a lot of a lot of useful functions okay let's go ahead and talk about associative arrays associative error is basically is a key value Pierce we are some darkest key corresponds to some value okay and let's create person associative array okay let's give it name to be bread the surname to be to be traversée let's give it an age of like 30 and the hobbies to be like no tennis and video games okay so and let's print the person name okay so we can access the name in the industry in this way in let's print also beer right here and here is the break let me comment all other code what we have above okay we have done working with normal areas let's work with associative arrays okay here I see bread so if I want to access like H I can't print it in the same way and I have that H so if I want to let's put this in the get area if I want to set some value to person like for example channel okay I'm gonna do this traversée media okay I can easily do this person channel is traversée media if I want to check if the area is like a specific key or not so I need I can use the same easy set function what I have mentioned before like person's age if it is set or not and that will be evaluated into true however if I try to like try to call person's address okay this will be evaluated into into false if I just want to print the keys of the person I need to use let me print this array underscore keys okay so we give here person and that returns the keys of the person associative array and here it is the person has name surname age hobbies in channel if I try to print the values of the associative array I need to use area underscore values I give the person and that prints the values of the person bred traversée here we have the age here we have another sub a ratings and videogames and we have traversing media okay so I also have possibility to sort the associative array by their keys or values or etc okay so I can use K sort give your person and that will modify the person associative array and if I dump the person right now the whole person array this will sort the person by keys so first I have age then I have channel then hobbies name and surname okay so if I want to sort it by keys but in the reverse order n to used K are sort and that returns in the reverse order I also have possibility to sort them by by values okay so like let's use a sort and that will sort it by values first will be bread then we'll be traverse a traverse media event then we'll be number and then we have the associative array and also have possibility to sort it by reverse order we are sort and this is how I see it so this AR sort is probably like more familiar with normal error is not probably with the associative errors but it still worked with associative arrays okay so let me show you one last example about arrays and that's that will be two-dimensional arrays so I actually can have arrays inside areas and that's image in the following case I have to do items which is an airy and I can have some like to-do items right here as a string but each to-do item has its own status and the text okay so I'm going to do sub-areas right here okay so like title of the to-do item will be item one in the it has also completed status and the completed status will be true or false okay and they can repeat the same thing multiple times so I have to do item 2 and this has completed such as false I have to divide m3 and it has completed status true and we can print test obviously these to-do items and this will be the result so we see an array of three elements these ones and each individual entry is also each individual element is also an array and it has two elements title and completed and that's all about Ares check out the link I will put in the video description and let's move on and talk about conditionals
Info
Channel: The Codeholic
Views: 2,106
Rating: undefined out of 5
Keywords: php, php tutorials, php tutorial, php course, php tutorial for absolute beginners, php tutorial for beginners, php tutorials for beginners, php tutorials 2020, php 7.4, learn php, php 2020 tutorial, php full course, TheCodeholic, php for beginners, php programming tutorial, php programming, php arrays, php arrays and loops, php arrays multidimensional, php associative array, php iteration of arrays, php basics, php array functions, php array tutorial, Part 7, part 7
Id: nR6bdjr2hxQ
Channel Id: undefined
Length: 21min 40sec (1300 seconds)
Published: Thu Apr 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.