array() function.
$phonebook = array ("Jack Bauer" => 98723489,
"Vesa Keskinen" => 1234567);
$test[0] = 12; $test['x'] = "hi!";print $test['x'];$colors = array(1 => 'Black', 'Blue', 'Yellow'); => $colors = array(1 => 'Black', 2 => 'Blue', 3 => 'Yellow');
$colors = array('Black', 'Blue', 'Yellow');
=>
$colors = array(0 => 'Black', 1 => 'Blue', 2 => 'Yellow');
true is transformed to integer 1 and
value false to 0
$test = array( "2" => "are",
3.4 => "you?",
true => "how",
false => "Hi,");
[] syntax:
$students = array("Jack", "Bill");
$students[] = "Lisa";
count()
and sizeof() (indentical)
$colors = array('white', 'blue');
$size = count($colors);
$colors = array('black', 'blue', 'red');
foreach ($colors as $color) {
print $color;
}
blackbluered
$persons = array('Jack Bauer' => 98723489);
foreach ($persons as $name => $number) {
print "$name, $number";
}
$colors = array('black', 'blue', 'red');
for($i=0; $i < count($colors); $i++) {
print $colors[$i];
}
in_array() function can be used
for searching values in an array.
$colors = array('black', 'blue', 'red');
if(in_array('black', $colors) {
...
}
array_search() function returns the
key of the found element.$row0 = array(1,2,3); $row1 = array(4,5,6); $row2 = array(7,8,9); $multidimensional = array($row0, $row1, $row2);
print $multidimensional[0][1];