Nov 13, 2011

Working with tables or arrays in PHP


Change the number of array elements

Now let's see some examples by which our arrays can increase or decrease the number of boxes available. 

Reduce the size of an array
array_slice ()
To reduce the number of boxes in an arrangement we have several functions. Among them, array_slice () we use when we want to cut some squares of the settlement, knowing the index of the boxes we want to keep.
It takes three parameters. The array, the index of the first element and the number of elements to be taken, the latter being optional parameter.
In the following example we have an array with four names. In the first run of array_slice () are indicating we want to take all elements from index 0 (the beginning) to a total of 3 items.
The second array_slice () indicates that all elements are taken from the index 1 (second box).
<?
$ Input = array ("Michael", "Pepe", "John", "July", "Paul"); 
/ / Modify the size $ Output = array_slice ($ input, 0, 3); / / Show the array foreach ($ output as $ current) echo $ today. "<br>";echo "<p>";/ / Modified again
$ Output = array_slice ($ out, 1);
/ / Show the array
foreach ($ output as $ current)
echo $ today. "<br>";
?>
Will output:
Miguel
Pepe
John
Pepe
John
array_shift ()
This function extracts the first element of the array and returns it. Also shortens the length of the array by removing the element that was in the first box. Always does the same, therefore not receive more than the array to which you want to delete the first position.
In the following code has the same vector with names and runs the function twice array_shift () removes an element at a time. Values ​​that are printed and the function returns the resulting array elements to remove the first box.
<?
$ Input = array ("Michael", "Pepe", "John", "July", "Paul");/ / Remove the first box
$ Out = array_shift ($ input);
/ / Show the array
echo "The function returns". $ Out. "<br>";
foreach ($ input as $ current)
echo $ today. "<br>";echo "<p>";/ / Remove the first box, now would be the second of the original array
$ Out = array_shift ($ input);
echo "The function returns". $ Out. "<br>";
/ / Show the array
foreach ($ input as $ current)
echo $ today. "<br>";
?>
Results in:
The function returns: Miguel
Pepe
John
July
Paul
The function returns: Pepe
John
July
Paul
unset ()
Is used to destroy a given variable. If arrangements can be used to remove a box an associative array (those without numerical indices but its index is a string).
Consider the following code to learn how to define an associative array and then delete one of your boxes.
<?
$ Estadios_futbol = array ("Barcelona" => "Nou Camp", "Real Madrid" => "Santiago Bernabeu", "Valencia" => "Mestalla", "Royal Society" => "Anoeta");/ / Show the stages
foreach ($ index as $ estadios_futbol => $ current)
echo $ index. "-". $ Today. "<br>";echo "<p>";/ / Remove the associated real madrid stadium
unset ($ estadios_futbol ["Real Madrid"]);/ / Show the stages again
foreach ($ index as $ estadios_futbol => $ current)
echo $ index. "-". $ Today. "<br>";
?>
The output is as follows:
Barcelona - Nou Camp
Real Madrid - Santiago Bernabeu
Valencia - Mestalla
Real Sociedad - Anoeta
Barcelona - Nou Camp
Valencia - Mestalla
Real Sociedad - Anoeta

Increase the size of an array

We also have at our disposal several features that can help us increase the number of boxes in an array.
array_push ()
Insert the end of the array a series of boxes that indicate you as parameter. Therefore, the number of cells in the array will increase in many items as you have indicated in the parameter of the function. Returns the number of squares of the resulting array.
Consider this code where you create an array and then added three new values.
<?
$ Table = array ("Lizard", "Spider," "Dog," "Cat," "Mouse");/ / Increase the array size
array_push ($ table, "Sparrow", "Paloma", "Bear");foreach ($ table as $ current)
echo $ today. "<br>";
?>
Results in this output:
Lizard
Spider
Dog
Cat
Mouse
Sparrow
Pigeon
Bear
array_merge ()
Now let's see how to join two arrays using array_merge function (). To this was passed two or more arrays as parameters and returns an array with all fields of vectors passed.
In this sample code we created three arrays and then merge with the function array_merge ()
<?
$ Table = array ("Lizard", "Spider," "Dog," "Cat," "Mouse"); $ Table2 = array ("12", "34", "45", "52", "12"); $ Table3 = array ("Sauce", "Pine", "orange", "Poplar", "Dog", "34");/ / Increase the array size
$ Result = array_merge ($ table, $ table2, $ table 3);foreach ($ result as $ current)
echo $ today. "<br>";
?>
Results in:
Lizard
Spider
Dog
Cat
Mouse
12
34
45
52
12
Willow
Pine
Orange
Poplar
Dog
34
One last thing. They can also introduce new cells in an arrangement by the usual methods of allocating new positions in the array to the boxes you need.
In normal arrays do so:
$ Table = array ("Sauce", "Pine", "orange");
$ Table [3] = "Algarrobo";
In associative arrays:
$ Estadios_futbol = array ("Valencia" => "Mestalla", "Royal Society" => "Anoeta");
$ Estadios_futbol ["Barcelona"] = "Nou Camp";
We will see later other possibilities of working with arrays.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes