Introduction to array in php programming languages. This is tutorial for arrays inside php Programming languages.Now a array is a way to store a data inside one variable its basically a collection of data.
code for basic array (Screenshot1):-
|
Screenshot1 |
<?php
$food = array('Pasta','Pizza','Salad');
echo $food[1];
?>
Output (Screenshot2):-
|
Screenshot2 |
For Example if i want to store a few numbers like 1, 5, 10, 20 if i want to store this inside one place where i can excess them that would be more useful then having to break them up somehow to use individually. It also works well when u want to loop through and take elements of array. In this example i had create an array by using a variable. I will do this by creating variable at standard and assigning array to it. Now $food is variable assigning to array with array keywords. Now values of array automatically assign key to the value of the array and key starts from zero and then it goes on and on. So, i had created array of food with values of array such as Pasta. Pasta will have key zero. In array you can also include integer, string but at one time if you using string you can’t mix with integer. So, values of array are Pasta , Pizza , salad. Now we have three elements to our array. At key zero pasta at key one pizza and at key two salad. If i want to excess elements of the array variable name with square brackets and elements key for the array. such as i.e $food[1] = Pizza.
code for basic array (Screenshot3):-
|
Screenshot3 |
<?php
$food = array('Pasta','Pizza','Salad');
echo $food[0];
?>
Output (Screenshot4):-
|
Screenshot4 |
Same example echoing array key zero i.e $food[0] = pasta.
code for basic array (Screenshot5):-
|
Screenshot5 |
<?php
$food = array('Pasta','Pizza','Salad');
print_r($food);
?>
Output (Screenshot6):-
|
Screenshot6 |
If we want to see all elements of the array i will use print_r function and put name of the array in and this gives us. See output now will see demonstration how array works and how it stores the values array keywords and also you can see every elements keys of the array.
If you write echo $food; it will only display array keywords for excess array you should write key of the array in square brackets.
Code for basic array(Screenshot7):-
|
Screenshot7 |
<?php
$food = array('Pasta', 'Pizza', 'Salad', 'Vegetable');
print_r($food);
?>
Output (Screenshot8):-
|
Screenshot8 |
In this example you can see output vegetable is added in elements of the array and also the key is automatically assign to it is key three.
So, in this way array works you can store many different values in one datatype inside one variable and it can be excess individually.
Code for basic array(Screenshot9):-
|
screenshot9 |
<?php
$food = array('Pasta', 'Pizza', 'Salad', 'Vegetable');
$food[4]='Fruit';
print_r($food);
?>
Output (Screenshot10):-
|
screenshot10 |
In this example you can see assign values in this way to the array. I had assign key four and Fruit. This methods is also used at some circumstances.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..