Tables

Tables

These are variables containing a structured set of variables. For those variables is accessed by a number in brackets given immediately after the variable name – the array. This number is called the index – the number of the variable in the array. The same is assigned a value to the array.

Example

<?php

$ Array[0] = „Entering number 0”;
$ Array[1] = „Entering number 1”;
$ Array[2] = „Entering the number 2”;

echo $array [2];                                        // Appears the inscription „Entering the number 2”;

?>

To add another entry to the end of the table simply by assigning values do not enter an index to the brackets. If in this way are added to the new table entries, the first entry has an index of 0.
The index can also be administered with a variable from another table or function – of any expression that returns a value.

Example

<?php

$Tab1[] = 1;
$Tab1[] = 0;
$Tab1[] = 3;
$Tab1[] = 2;

$Tab2[] = „first”;
$Tab2[] = „second”;
$Tab2[] = „Third”;
$Tab2[] = „fourth”;

echo $tab2[$ tab1 [2]];

?>
An array element can be any type of variable (including other arrays and objects)

An associative array
There is also another kind of tables   –   associative arrays. These are the tables in which the numerical index, instead of using the character ID (key):

Example

<? php

$array [„name”] = „Tom”;
$array [„lastname”] = „Smith”;
$array [„address”] = „New York”;

echo $array [„name”]. ‚”. $array [” name „].”, „. $array [” address „].” n „;

?>

Viewing tables
There is often a need to perform an operation on all elements of the array. If the array is a simple array of numerical indices and know the amount of these elements:

Example

<?php

$Tbl [] = 1;
$Tbl [] = 2;
$Tbl [] = 3;
$Tbl [] = 4;
$Tbl [] = 5;

for ($x = 0; $x <5; $x ++) {                      // loop is executed 5 times (0 … 4)

echo $tbl [$ x];

}

?>

If you do not know the amount of elements in the array we use the „count” ($nazwa_tablicy). It returns the number of elements in the array specified as a parameter.
Example

<?php

$tbl [] = 1;
$tbl [] = 2;
$tbl [] = 3;
$tbl [] = 4;
$tbl [] = 5;

for ($x = 0, $cnt = count ($tbl); $x <$cnt; $x ++) {

echo $tbl [$x];

}

?>

If it is necessary to review the hash, then you should use the „list” () and „each” (). When passing through associative arrays need to use a while loop.

Example

<?php

$array [„name”] = „Tom”;
$array [„lastname”] = „Smith”;
$array [„address”] = „New York”;

while (list ($key, $value) = each ($array))
echo „$key => $value <BR>”;

?>
In each iteration of the loop, we have available 2 variables, the host of the next keys and values assigned to the keys.

 

Sorting tables
PHP has a whole set of functions for sorting arrays. These are:
• asort ()
• arsort ()
• ksort ()
• rsort ()
• sort ()
• uasort ()
• usort ()
• uksort ()
Most functions (except the last three) takes one parameter: a variable containing the array to be sorted. None of the function does not return any result.

asort ()
sorts of associative arrays while maintaining the assignment of keys to values

<?php

$fruit = array ( „d” => „mango”, „a” => „papaya”, „b” => „banana”, „c” => „orange”);
asort ($fruits);
reset ($fruits);                                         // This function returns the first element of the array
while (list ($key, $value) = each ($fruits)) {
echo „$key = $valuen”;
}

?>
The result of the above example should be:

c = orange
b = banana
d = mango
a = papaya

arsort ()
sorts in reverse order associative arrays while maintaining the assignment of keys to values. The almost identical to the previous one, so that the data is sorted „from behind.”

ksort ()
sorts of associative arrays of keys. The above example after swapping function asort on ksort should give the following result:

a = papaya
b = banana
c = orange
d = mango

rsort ()
sorts regular tablets (not associative) in reverse order

sort ()
sorts regular tablets (not associative) in alphabetical order
uasort ()
function sorting associative arrays using a user-defined comparison function elements (function name is given by the second parameter)

usort ()
sort function ordinary arrays using a user-defined function

uksort ()
function sorting associative arrays by key with the user-defined function.

In the last three functions you need sorting as a second parameter to specify the function of comparing the elements of the array. Such functions take two arguments. 0 is returned if the arguments are equal, -1 if the first argument is less than 1 second and if it is greater.

Creating a string arrays and vice versa
PHP allows you to replace strings to arrays and vice versa. Swapping over to the table is very useful when you need to draw a piece of data from the line. Suppose that we read in the data file line from the log recorded by the counter Website: „10/11/2011, 19: 23: 36; Netscape Navigator; 192.168.1.1”. As you can see the data are separated by semicolons. To separate the strings on the table is a function explode (). As the first parameter you have to give it a character or a long string that separates the different fields, as a second string to separate. Optionally, you can specify a third argument, which is the maximum number of fields – if there is more than this number, the latter will contain all of the other fields. The function returns an array containing the following fields.
<?php

$data = „10/11/2011, 19: 23: 36; Netscape Navigator; 192.168.1.1″;
$array = explode (”, $ data);

?>

There is also an extended version of the function explode: split (). It differs in that instead of a simple string separating field, it accepts regular expressions.
Sometimes action is needed in the other direction to have joins an array of fields into a single string, in which fields are separated by a character (or more). That’s what the function implode (). The first parameter is given a sequence whereby „glued” are elements of the array, and the second board just to be cobbled. Is returned a string that contains elements of stuck. As an example of the application can be used just write data to the user application hit counter – array contains data about visitors, and you need a string separated by semicolons.
Example

<?php

$data = implode (”, $ array);

?>

Cookies help us deliver our services. By using our services, you agree to our use of cookies. More Info | Close