1. Layout of arrays
Arrays are very important to php. Form data and URL variables are saved in arrays. Values from a database are saved in arrays. Everything what is something like a mathematic function can be build with arrays. Per defintion an array is an ordered list (does not mean they are sorted) of pairs which each pair got a key and a value. These keys must be only integer numbers or strings. However the values can be everything, even arrays. A single array value can be use like a variable.
2. Usage of arrays
Arrays can be created with the language construct array. The values of the array are given as parameters.
<?php
$arr = array("foo", "bar", "bla", 5.6, false, -10, "foo", "foo", "bar", "foo");
?>
This array got 10 values. The keys, sometime called indexes, are generated
automatically, starting by 0 ascending. The first value gets the index
0 and the last one gets the index 9. This can be checked
with var_dump.
array(10) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "bla"
[3]=>
float(5.6)
[4]=>
bool(false)
[5]=>
int(-10)
[6]=>
string(3) "foo"
[7]=>
string(3) "foo"
[8]=>
string(3) "bar"
[9]=>
string(3) "foo"
}
To get such a field use the array and brackets for the index.
<?php
$arr = array("foo", "bar", "bla", 5.6, false, -10, "foo", "foo", "bar", "foo");
echo $arr[0]; // outputs foo
echo $arr[3]; // outputs 5.6
echo $arr[4]; // outputs nothing as the string representation of bool(false) is an empty string
var_dump($arr[4]); // outputs bool(false);
?>
As you see its possible to have same values in the array as they got different keys.
So there are 4 array entries with the value foo at the indexes
0, 6, 7 and 9.
The value can be changed as you want if you replace the value with all the operators you know.
<?php
$arr = array("one", "two");
$arr[1] = "five";
?>
If there is already an array entry with the given index the old value will be overwritten. If there isn't such an array entry with the given index a new array entry is created with the given key and value. So its possible to create an empty array and fills it with keys and values later on.
<?php
$arr = array();
$arr[3] = "value";
$arr[9] = "another value";
var_dump($arr);
?>
The var_dump creates the following output.
array(2) {
[3]=>
string(4) "value"
[9]=>
string(13) "another value"
}
As you see we got an array of the size of 2 entries, with the indexes 3
and 9 and the values value and another value.
As mentioned the array is not sorted but there are a lot of sorting functions to do so.
Instead of numbers you can use strings as keys.
<?php
$user = array();
$user['name'] = 'John Doe';
$user['age'] = 18;
$user['home'] = 'Somewhere';
echo 'My name is '.$user['name'].', I am '.$user['age'].' old and living in '.$user['home'].".\n";
var_dump($user);
?>
Die Ausgabe ist dabei wie folgt:
My name is John Doe, I am 18 old and living in Somewhere.
array(3) {
["name"]=>
string(8) "John Doe"
["age"]=>
int(18)
["home"]=>
string(9) "Somewhere"
}
Arrays which use strings as indexes are called associative arrays. All other kind of arrays are called just arrays or nummeric arrays.
If you assign a value to an array entry without an index given (as in $arr[])
the key is automatically generated as the biggest index number +1, but at least
the value 0.
<?php
$foo = array();
$foo[] = "value"; // Index 0 is used
$foo[] = "value"; // Index 1 is used, biggest index was 0
$foo[10] = "value"; // Index 10 is used as given
$foo[] = "value"; // Index 11 is used, biggest index was 10
var_dump($foo);
$foo = array();
$foo[-5] = "value"; // Index -5 is used as given
$foo[] = "value"; // Index 0 is used, biggest index was -5 but at least 0 is used
var_dump($foo);
?>
The following output is generated.
array(4) {
[0]=>
string(5) "value"
[1]=>
string(5) "value"
[10]=>
string(5) "value"
[11]=>
string(5) "value"
}
array(2) {
[-5]=>
string(5) "value"
[0]=>
string(5) "value"
}
The indexes can also be assigned during the array construct.
Instead of writing the value you can pretend the value with x => where
x is the index for the value.
<?php
$bar = array(5 => "bar", "foo");
// 2. value gets the index 6
$var = array(-10 => "abc", "xyz");
// 2. value gets the index 0
$var = array("name" => "John Doe", "foobar");
// 2. value gets the index 0
?>
To delete an array entry use the funktion unset.
<?php
$arr = array();
$arr[] = "foo";
$arr[] = "bar";
$arr[] = "xyz";
unset ($arr[1]); // the entry "bar"
var_dump($arr);
?>
The output shows the entry with the index 1 is deleted, however the
indexes 0 and 2 still exists.
array(2) {
[0]=>
string(3) "foo"
[2]=>
string(3) "xyz"
}
You can even use the compare operators == and ===, and
the + operator. To arrays are the same (==) if they
match in the same array entries and are identically (===) if they
got the array entries in the same order and of the same type.
<?php
var_dump(array("x", "y") == array(1 => "y", 0 => "x")); // bool(true)
var_dump(array("x", "y") === array(1 => "y", 0 => "x")); // bool(false)
?>
You can merge array together with the + operator. Indexes
which are already used are not overwritten. If you really want to
merge all values together use a function like array_merge.
<?php
var_dump(array("x") + array("y"));
// an array with just one value 'x' as both temporary arrays use the same key 0
var_dump(array_merge(array("x"), array("y")));
// an array with both values 'x' and 'y'
?>
3. Foreach loop
For arrays there exists a special loop called foreach. You can use
it to iterate over all array entries. A foreach loop starts
with the keyword foreach. Then a opening parenthese is followed with
the array or variable to iterate on. After that the keyword as is followed
with a new variable. After you use the closing parenthese the loop body followes.
<?php
$a = array("foo", "bar", "bla");
foreach ($a as $value) {
echo $value."\n";
}
// outputs the value of the array
?>
If you need also the keys for each iteration pretend before your new variable
the code $var =>.
<?php
$user = array('name' => "John Doe",
'age' => 18,
'home' => 'Somewhere',
10 => 100);
foreach ($user as $k => $v) {
echo "Array value with the index '".$k."' got the contents: ".$v."\n";
}
?>
The indexes are used if you want to change the array inside the loop. The
loop variables ($k and $v in the last example)
are only copies of the origin array values. If you want to change a
value in the array use the normal array[index] syntax.