Manual of PHP

 
 
 

Web map

 
rim   rim
Carriedarrow Counterfoils (array)

 

Counterfoils (array) To print E-mail

In the achievement of a script in PHP in multiple occasions there exist variables that have similar information and are processed of similar form. For it PHP (and other languages) possesses an element so called array. An array is a set of variables grouped down the only name. Every variable inside the counterfoil is named an element. Inside the same counterfoil variables of different types can exist and it is not necessary that they are all of the same type.

It is necessary to differ between two types of existing counterfoils:
- Index-linked: That one which access to the elements is realized by the position that they occupy inside the structure (0 always begin from the position). Example: $amigos [0]
- Associative: It is that in that the elements key - value are formed by pairs and the access is realized providing a certain key. Example: $amigos ['age']

To create counterfoils in PHP two forms exist:

- Of implicit form, which would consist of indicating him the element (be already providing his position or his key).Ejemplo: $nombres [0] = 'Javier';
In case of not indicating him a position the array will take the value following the last introduced value. Example: $nombres [] = 'Lucas'//would take 1 as a value since the introduced last thing 0 was.

- By means of array () in which we spend the elements to him as parameters. In case of index-linked counterfoil they take the position that they occupy in the creation of the counterfoil, while those of the associative counterfoil his value is assigned to them by means of "=>".Ejemplo: $amigo=array ('Name' => 'Jose', 'Direction' => 'Neopatria 21');

It is necessary to emphasize that not alone PHP limits himself to the counterfoils existence himself but counterfoils counterfoils exist, or what it is the same, multidimensional counterfoils. Example: $amigos [2] ['Pedro']

Trip of a counterfoil

We have diverse hardware to be able to gain access to the elements of a counterfoil. In every moment there is supported a reference of the element of the counterfoil to which access is had, therefore, to cover a counterfoil it will be enough to modify the above mentioned reference. In case of an index-linked counterfoil the trip will be realized by means of a ringlet and for it we must know the number of entire elements that possesses the counterfoil. For it we base of the function count (variable) where variable it represents the variable of the one that loves to obtain the number of elements. If variable is a counterfoil he returns the number of elements that it has, returns 1 if only it has an element (although it is not a counterfoil) and 0 if it does not have any value.

Another function that allows us to know the number of elements is sizeof (counterfoil).
To gain access to the elements of an associative counterfoil we must use the function ecah () that recovers the pair formed by key and value and also it advances a leader position. His syntax is each (counterfoil) and the values that the associative counterfoil returns are the following ones:

Key Meaning
0 Name of the key
1 Associative value of the key
key Name of the key
I valued Value associated with the key

The function that the builder realizes list (variable1, variable2... variableN) is to assign the values of the current element of a counterfoil to the variables indicated like parameter.

Navigation on counterfoils

When the navigation talks each other of index-linked counterfoils it is simple since only he is enough to gain access to the element that we want to show, but on having talked each other of some associative counterfoil, it is not possible to apply the same treatment. For it there exists a set of prefabricated functions that allow us to realize multitude of actions:

Syntax Action
reset (counterfoil); The internal leader returns to the first position
end (counterfoil); The internal leader goes to the last position
next (counterfoil); The leader goes to the following element
prev (counterfoil); He gains access to the previous element
current (counterfoil); He returns the content of the current element

Elements insertion

For the elements insertion inside an array there exists a series of functions that allow us to add elements. Between them we stand out:

array_push (counterfoil, variable1, variableN);
He adds elements at the end of the function and his length will increase so many elements as they have been added

array_unshift (counterfoil, variable1, variableN);
He adds that elements at the beginning of the function displacing to others so many positions as elements there should be.

array_pad (counterfoil, nuevo_tamaño, valor_relleno);
The size of the counterfoil increases using a proportionate value as stuffed.

Elimination of elements

array_shift (counterfoil);
It eliminates the first element of the counterfoil

array_pop (counterfoil);
It eliminates the last element of the counterfoil

array_splice (entry, pos_ini, [size], [substitutes]);
It is used to replace or to erase the content of a portion of counterfoil, for it we must specify the position from which we want to initiate the erasure or replacement, the size or number of elements that will turn out to be affected and the substitutes (in case that we want to replace it with some element).

array_keys (counterfoil, [looked value]);
It is used when we want to eliminate an element which position we do not know.

array_values (counterfoil);
He returns a counterfoil index-linked with all the values stored in the counterfoil spent for parameter.

Massive counterfoils manipulation

array_walk (matriz,nombre_de_la_funcion,lista_parametros);
It is used to realize the same process defined in the function in all the included elements.

Subcounterfoils securing

array_slice (counterfoil, position, size);
It allows to extract a sequence of elements of a counterfoil. The parameters to be spent him are the counterfoil in which we want to extract the above mentioned elements, the position from which there begins the extraction and the size of the extraction (positions that we include from the initial).

Counterfoils arrangement

Criterion Function
Ascending order (index-linked counterfoil) sort (counterfoil)
Descending order (index-linked counterfoil) rsort (counterfoil)
Ascending order for value (associative counterfoil) asort (counterfoil)
Descending order for value (associative counterfoil) arsort (counterfoil)
Ascending order for key (associative counterfoil) ksort (counterfoil)
Descending order for key (associative counterfoil) krsort (counterfoil)


Other functions

In this paragraph they will comment on a series of functions (not all because it would be impossible) that can serve to us in certain moment.

compact () He Returns an associative counterfoil from an indeterminate number of parameters
extract () It Creates variables from associative counterfoil
array_unique () He Returns counterfoil without repeated information since some of them are eliminated
array_reverse () He Returns counterfoil with the same elements but in inverse order
shuffle () It Modifies the order of elements of random form
array_count_values () He Returns an associative counterfoil that contains frequencies of repetition of the values of the counterfoil
in_array () he Allows to verify if a value is in the counterfoil
array_merge () It Combines elements of two counterfoils in 1.

Previous chapter:
Code inclusion from a file

Following chapter:
Chains of Characters



 

 
Front
Chapters of the Manual of PHP
Introduction to PHP
Variables
My First Script PHP
Operators in PHP
Structures of Control
Functions in PHP
Code inclusion
Counterfoils (array)
Chains of Characters
Classes
Dates
Entry and Exit
Operations with Files
The language SQL and PHP
Connection with MySQL
Meetings
Forms with PHP
To practise in line
List of practices in line
FAQ
Frequent questions
Codes PHP
List of Codes PHP
Forums
Forums PHP
Other Manuals
Manuals of other languages
 
   
 
 
MySpaceScripts.info - Sitemap - Contact us - Sitemap - Contact us - Sitemap - Contact us

auschwitz tour , suché víno , waagen , car.nice-car.co.uk , Website designer Dublin , Cool QSL Print Service , Panasonic