PHP Arrays Flash Cards

Play Memory | Create Card File | Append to Card File
Title: PHP Arrays
Description: Zend PHP 5 Certification - Arrays
Number of Cards: 10
Save Count: 0
Author: john.godwin
Created: 2012-01-04
Tags: php
Private No

Save Count represents the number of people who have saved this card set to their flashcard list. Consider this an endorsement!

    • Question
    • Answer
    • Side 3
    • What are arrays?
    • They are ordered collections of items, called elements. Each element has a value and is identified by a unique key.
    • P 47
    • What are the two ways in which an array can be created?
    • By calling the array() construct which can be passed a series of values, and optionally keys.

      Examples:
      $a = array(10,20,30); //keys are automatically assigned, starting at 0
      $a = array("a' => 10, 'b' => 20, 'cee' => 30);
      $a = array(5 => 1, 3 => 2, 1 => 3,); //"dangling comma has no effect on array
      $a = array(); //empty array

      The other method is to user the array operator.

      Example:
      $x[] = 10; //next highest numeric key assigned
      $x['aa'] = 11;
    • P 47 - 48
    • What are two functions PHP uses to output a variable's value (such as an array) recursively?
    • var_dump($var1, $var2, etc...);

      Will recusively print out the contents of a scaler and composite value(s), along with their data type.

      print_r($var);
      OR
      $output = print_r($var, true);

      Print out the contents of a composite or scaler variable, but NOT its type. Also, has option to return string to a variable.
    • P 48 - 49
    • Arrays can be roughly divided into two categories: enumerative and associative. What does this mean?
    • Enumerative: Indexed only using numerical values.

      Associative: Associate an arbitrary key to every element (sometimes referred to as dictionaries).
    • P 49
    • What will be the next numerical key for the following array:

      $a = array("2" => 5);
    • 3. "2" can be converted to an integer since it is a traditional decimal representation. If it were "02" or "two", the next key would be 0.
    • P 49 - 50
    • How would you create a multi-dimensional array in PHP?
    • Example:

      $array = array();

      $array[] = array(
      'foo',
      'bar'
      );
      $array[] = array(
      'baz',
      'bat'
      );

      echo $array[0][1] . $array[1][0]; //print barbaz
    • P 50
    • What does the list() construct does in the following code:

      $info = array('coffee', 'brown', 'caffeine');
      list($drink, $color, $power) = $info;
    • $drink will = coffee
      $color will = brown
      $power will = caffeine

      Useful when returning arrays from data rows in a database.

      Example:

      $result = mysql_query("SELECT id, name, salary FROM employees", $conn);
      while (list($id, $name, $salary) = mysql_fetch_row($result)) {
      echo " <tr>\n" .
      " <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
      " <td>$salary</td>\n" .
      " </tr>\n";
      }
    • P. 50 - 51
    • What does the addition operator do in the following example:

      $a = array("a" => "apple", "b" => "banana");
      $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

      $c = $a + $b; // Union of $a and $b
      echo "Union of \$a and \$b: \n";
      var_dump($c);
    • Prints:

      Union of $a and $b:
      array(3) {
      ["a"]=>
      string(5) "apple"
      ["b"]=>
      string(6) "banana"
      ["c"]=>
      string(6) "cherry"
      }
    • P. 51 - 52
      http://www.php.net/manual/en/language.operators.array.php
    • When comparing arrays using the "==" and "===" operators, what is the difference?
    • "==" will look at both the keys and the values of both arrays, return true if they exist both exist no matter what the order is.

      "===" does the same as above, but the keys must be in the same order to return true
    • P. 52 - 53
    • What does the count() function do for an array?
    • Count() will return the number of elements in an array.

      Example:
      $a = array (1, 2, 3);
      echo count($a); //Prints 3

      Note, when count is used on a scaler value, it will always return 1. To determine if a variable is an array, use is_array() instead.
    • P. 53