Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
en:create:array [09.11.2014 12:41] alexander.ritteren:create:array [26.10.2018 09:54] – [Arrays] rafael.wilms
Line 1: Line 1:
 ====== Introduction to Arrays ====== ====== Introduction to Arrays ======
  
-Arrays are an extremely useful tool in PHP. They are predominately used when filters, rotation, or analysis cannot be carried out by using standard functions. You can find concrete examples of the PHP code used in their respective chapters, e.g. [[:en:create:rotation|Rotation]], [[:en:create:points|Count Points for Answers]], [[:en:create:filter-items|Use Selected Items Another Question]].+Arrays are an extremely useful tool in PHP. They are predominately used when filters, rotation, or analysis cannot be carried out by using standard functions. You can find concrete examples of the PHP code used in their respective chapters, e.g. [[:en:create:rotation|Rotation]], [[:en:create:points|Count Points for Answers]], [[:en:create:filter-items|Use Selected Items In Another Question]].
  
 ===== Arrays ===== ===== 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: 18.11.2020 17:30 by sophia.schauer
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki