A variable type already described but can be relatively difficult to assimilate about most are the arrays. An array is a variable that is composed of several elements each listed within itself through a key.
In the previous chapters gave the example of an array called sense containing the different human senses:
$ Direction [1] = "see";
$ Direction [2] = "touch";
$ Direction [3] = "hear";
$ Direction [4] = "like";
$ Direction [5] = "smell";
In this case the array lists its elements, commonly called values by numbers. The numbers from 1 to 5 are therefore the keys and the senses are the associated values. Nothing prevents us from using names (strings) to classify them. The only thing we do is quotes:
<?
$ Currency ['Spain'] = "Peseta";
$ Currency ["France"] = "Franco";
$ Currency ["use"] = "Dollar";
?>
Another way to define identically the same array and that can help us to create more complex arrays is the following syntax:
<?
$ Currency = array ("Spain" => "Peseta", "France" => "Franco", "uses" => "Dollar");
?>
A convenient way to store data is by creating multidimensional arrays (tables). Take the following example: We want to store in a single table name, currency and language spoken in each country. To do this we use an array called country that is defined by three characteristics (keys). To create it, we should write an expression of the same type as that seen previously in an array that will put it inside the other. This process will include a statement within another is called nesting and is very common in programming:
<?
$ Country = array
(
'Spain' => array
(
"Name" => "Spain",
"Language" => "Castilian"
"Currency" => "Peseta"
)
"France" => array
(
"Name" => "France",
"Language" => "French",
"Currency" => "Franco"
)
);
echo $ country ['Spain'] ['currency'] / / Draw on screen: "Peseta"
?>
Run script
Before going into detail on this little script, we discuss some points concerning the syntax. As you can see, this script sequence, we have introduced the semicolon ";" at the end of each line. This is simply because we have written can be considered as a single statement. In fact, we decide to cut it into several lines, thus, easier to read. True instruction would completely defined once the array and that is where we put the single semicolon. On the other hand, you can see how we played with the tab to separate lines more than others from the beginning. This will also make for clarity as we can see what parts of the code are included inside each other. It is important to get used to writing in this way the same way to introduce the comments and that clarity is essential scripts when debugging. A bit of effort when creating them can save many hours when correcting or months later.
Turning now to review the program, as you can see, this allows us to store tables and, from a simple request, view a certain value on the screen.
What is interesting is that the utility of the arrays does not end here, but we can also use a variety of features designed to sort by alphabetical order or reverse, by key, count the number of elements of the array in addition to inside move it forward or backward.
There are many functions proposed for the treatment of PHP arrays, we will not enter here into a description of them. Just include this small table that can be supplemented, if necessary, with the documents already mentioned.
Function | Description |
array_values (mi_array) | List the values contained in mi_array |
asort (mi_array) and arsort (mi_array) | Sort by alphabetical order or reverse depending on the values |
count (mi_array) | We are the number of elements in our array |
ksort (mi_array) and krsort (mi_array) | Sort by alphabetical order or reverse depending on the key |
list ($ variable1, $ variable2 ...)= mi_array | Assign each variable to each of the values of the array |
next (mi_array), prev (mi_array), reset (mi_array) and end (mi_array) | Allow us to move inside the array with a pointer forward, back and at the beginning and end. |
each (mi_array) | Gives us the courage and the key element on which we stand and move the pointer to the next item. |
Very useful is also the loop foreach that runs through the array sequentially from beginning to end.
To supplement this information will be of great interest the article Working with tables or arrays in PHP and for those who prefer the video training, we recommend watching the videos on PHP arrays .
0 comments:
Post a Comment