Cursussen/Courses Codesnippets     Top 
PHP - Arrays


1. Structure
To use a list of values in a program, you can put those values in an array.
Each value is identified by the array name and an index or key.
The index can be a number or a string and is enclosed in brackets ([ ]).
A memory location is reserved for each value in the computer's memory.
<?php
$dagnamen = array("maandag", "dinsdag", "woensdag","donderdag","vrijdag","zaterdag","zondag");
echo $dagnamen[0];	// eerste item van de array dagnamen (index begint bij 0)
echo "<br>";
echo $dagnamen[4];	// vijfde item wordt getoond


2. Use
You can initialize an array with the array() function. An empty array is then created.
When entering you can take a number as index. You set the array item equal to a value by writing the name of the array, then using parentheses to enclose the index, and then typing the contents after the equals sign.
With a For loop or a foreach loop you can go through the items of the array and display the index and contents or use them further in your program.
If you fill in the array with a word as index, you use an arrow between the index and the content (=> a combination of a is equal to sign and a greater than sign).
You can also use an array as content and thus compose an array of arrays. Each array can then in turn be an array of arrays. You use a loop for each array to retrieve, display or use all the data.
Some examples:
<?php
// eenvoudige lijst
$talen = array();
$talen[0] = "Nederlands";
$talen[1] = "Frans";
$talen[2] = "Engels";
for ($i = 0 ; $i < count($talen) ; $i++) {
	echo "index = " . $i . " inhoud = " . $talen[$i] . "<br>";
}
echo "<br>";
?>
<style>
table, tr, td, th {
	padding: 2px;
	border:1px solid blue;
	border-collapse:collapse;
}
th {
	background-color: yellow;
	color: black;
}
</style>
<table>
<tr>
	<th>fruitnaam</th><th>naam</th><th>kleur</th>
</tr>
<?php
// 2 array's
$appels = array(
			array("naam"=>"jonagold","kleur"=> "rood"),
			array("naam"=>"golden delicious","kleur"=> "geel"));
$bananen = array(
			array("naam"=>"chiquita","kleur"=> "geel"),
			array("naam"=>"rode banaan","kleur"=> "rood"));
// een array van array's
$fruit = array("appels"=>$appels,"bananen"=>$bananen);
foreach($fruit as $index => $item) {
	foreach($item as $sleutel => $waarde) {
		echo "<tr>";
		echo "<td>" . $index . "</td>";
		foreach($waarde as $key => $value) {
			echo "<td>" . $value ."</td>";
		}
		echo "</tr>";
	}
}
?>
</table>


3. Functions
You can use standard PHP functions with arrays.
The count() function counts the number of items in the array (see simple list in previous section).
You can merge two arrays with the array_merge() function.
Sorting elements of an array can be done with a sort() function.
With the function in_array() you can check whether a value exists as content in the array.
You can check if a key value is present in the array with the array_key_exists() function.
echo "<br>";
if (in_array("granny smith",$appels)) {
	echo "er is een granny smith in de appels array!<br>";
} else {
	echo "er is GEEN granny smith in de appels array!<br>";	
}
if (array_key_exists("bananen",$fruit)) {
	echo "er is een bananen array in de fruit array<br>";
} else {
	echo "er is GEEN bananen array in de fruit array<br>";	
}


4. Multidimensional
You can set multiple levels in an array by placing multiple indexes in brackets after the array name. This allows you to compose a matrix, for example.
In the example below, a translation in a different language is maintained for each word.
<?php
$woorden["stoel"]["Frans"]  = "chaise";
$woorden["stoel"]["Engels"]  = "chair";
$woorden["chaise"]["Nederlands"]  = "stoel";
$woorden["chaise"]["Engels"]  = "chair";
$woorden["chair"]["Frans"]  = "chaise";
$woorden["chair"]["Nederlands"]  = "stoel";
// vertaling van chaise in het Engels
echo "vertaling van chaise = " . $woorden["chaise"]["Engels"] . "<br>";
// zoeken naar vertaling van tafel in het Engels
if (isset($woorden["tafel"]["Engels"])) {
	echo "zoeken naar vertaling van tafel: " .$woorden["tafel"]["Engels"];
} else {
	echo "zoeken naar vertaling van tafel: " . " niet gevonden";
}