Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:create:array [09.11.2014 15:18]
alexander.ritter
en:create:array [26.10.2018 09:54]
rafael.wilms [Arrays]
Line 37: Line 37:
  
 <code php> <code php>
-$a = array()+$a = array();
 for ($i=0; $i<10; $i++) { for ($i=0; $i<10; $i++) {
   $a[] = $i + 1;   $a[] = $i + 1;
Line 171: Line 171:
  
 ''​array_merge()''​ (join) and ''​array_intersect()''​ (intersection) are similarly useful. ​ ''​array_merge()''​ (join) and ''​array_intersect()''​ (intersection) are similarly useful. ​
 +
 +
 +===== Associative Arrays =====
 +
 +Normal arrays are simply a list of elements. Each element has a unique position, marked by the index (''​0'',​ ''​1'',​ u.s.w). The index is also known as //key//.
 +
 +You create an associative array by explicitly setting the key. For assigning values to keys, PHP uses the characters ''​%%=>​%%''​. You __may__ use the well known index as a key -- arrays ''​$a''​ and ''​$b''​ in the subsequent PHP code are equivalent.
 +
 +<code php>
 +$a = array(
 +  '​one',​
 +  '​two',​
 +  '​three'​
 +);
 +
 +$b = array(
 +  0 => '​one',​
 +  1 => '​two',​
 +  2 => '​three'​
 +);
 +</​code>​
 +
 +Yet, keys are not limited to numbers -- you may as well use strings. The next array defines the correct answers for a row of knowledge questions (question type "​selection"​).
 +
 +<code php>
 +$c = array(
 +  '​AB01'​ => 2,
 +  '​AB02'​ => 1,
 +  '​BB01'​ => 5
 +);
 +</​code>​
 +
 +To access thee element of an associative array, just specify the key in square brackets:
 +
 +<code php>
 +$x = $c['​AB01'​];​
 +// also possible
 +$key = '​AB01';​
 +$x = $c[$key];
 +</​code>​
 +
 +When working with associative arrays, the PHP internal function ''​[[http://​php.net/​manual/​en/​function.array-keys.php|array_keys()]]''​ as well as the FOR loop ''​[[http://​php.net/​manual/​en/​control-structures.foreach.php|foreach]]''​ are very helpful. The following PHP code checks, if a correct answer was given to multiple quesitons ([[:​en:​create:​points|Count Points for Answers]]).
 +
 +<code php>
 +$c = array(
 +  '​AB01'​ => 2,
 +  '​AB02'​ => 1,
 +  '​BB01'​ => 5
 +);
 +foreach ($c as $label=>​$correct) {
 +  if (value($label) == $correct) {
 +    html('<​p style="​color:​ green">​The answer to '​.$label.'​ is correct.</​p>'​);​
 +  } else {
 +    html('<​p style="​color:​ red">​The answer to '​.$label.'​ is wrong.</​p>'​);​
 +  }
 +}
 +</​code>​
 +
 +Using ''​array_keys()''​ also allows regular FOR loops:
 +
 +<code php>
 +$c = array(
 +  '​AB01'​ => 2,
 +  '​AB02'​ => 1,
 +  '​BB01'​ => 5
 +);
 +$fragen = array_keys($c);​
 +for ($i=0; $i<​count($fragen);​ $i++) {
 +  $label = $fragen[$i];​
 +  $correct = $c[$label];
 +  if (value($label) == $correct) {
 +    html('<​p style="​color:​ green">​The answer to '​.$label.'​ is correct.</​p>'​);​
 +  } else {
 +    html('<​p style="​color:​ red">​The answer to '​.$label.'​ is wrong.</​p>'​);​
 +  }
 +}
 +</​code>​
 +
  
 ===== Further Array Functions ===== ===== Further Array Functions =====
  
 There is a whole range of further commands used for arrays. You can find a complete list in the official [[http://​de3.php.net/​manual/​en/​ref.array.php|PHP Manual: Array Functions]]. There is a whole range of further commands used for arrays. You can find a complete list in the official [[http://​de3.php.net/​manual/​en/​ref.array.php|PHP Manual: Array Functions]].
en/create/array.txt · Last modified: 26.10.2018 09:54 by rafael.wilms
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki