Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

PHP - Basic introduction to arrays

417 vues

Publié le

This content presents a introduction of how the arrays works in PHP. It is also a content to the people who wants to take the ZCE exam from Zend and be certified.

Although this content doesn't has all operations, functions and so on in PHP as the title said is a introduction.

Publié dans : Technologie
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

PHP - Basic introduction to arrays

  1. 1. Arrays basics Arrays Enumeratives vs Arrays Associatives
  2. 2. Quiz - 1 $a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ]; $b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]; a. $a is enumerative and $b is associative b. $b is enumerative and $a is associative c. $a and $b are enumeratives d. $b and $a are associatives
  3. 3. Quiz - 1 $a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ]; $b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]; a. $a is enumerative and $b is associative b. $b is enumerative and $a is associative c. $a and $b are enumeratives d. $b and $a are associatives
  4. 4. Array operation + vs array_merge
  5. 5. Quiz - 2 $a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ]; $b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]; var_dump($a + $b); a. [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’, 3 => ‘A’, 4 => ‘B’, 5 => ‘C’ ] b. [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2, 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ] c. [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’, ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]
  6. 6. Quiz - 2 $a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ]; $b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]; var_dump($a + $b); a. [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’, 3 => ‘A’, 4 => ‘B’, 5 => ‘C’ ] b. [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2, 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ] c. [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’, ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ]
  7. 7. Quiz - 3 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(array_merge($a, $b)); a. [ 0 => 1, 1 => 2, 2 => 3, 3 => 1, 4 => NULL ] b. [ 0 => 1, 1=> 2, 2 => 3 ] c. [ 0 => 1, 1=> 2, 2 => NULL ]
  8. 8. Quiz - 3 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(array_merge($a, $b)); a. [ 0 => 1, 1 => 2, 2 => 3, 3 => 1, 4 => NULL ] b. [ 0 => 1, 1=> 2, 2 => 3 ] c. [ 0 => 1, 1=> 2, 2 => NULL ] With enumerative arrays the array_merge will just append in a new array every element. As a result you have a big array with all values but without the old keys. The exception is with the associative arrays. The array merge will overwrite a value key with the last value.
  9. 9. Comparing arrays == vs === and != vs !==
  10. 10. Quiz - 4 $a = [ 1, 2, 3 ]; $b = [ 2 => 3, 1 => 2, 0 => 1 ]; var_dump($a == $b); var_dump($a === $b); a. true true b. true false c. false false d. false true
  11. 11. Quiz - 4 $a = [ 1, 2, 3 ]; $b = [ 2 => 3, 1 => 2, 0 => 1 ]; var_dump($a == $b); var_dump($a === $b); a. true true b. true false c. false false d. false true == compare just the value and key regardless the order === compare the value, key and order
  12. 12. Quiz - 5 $a = [ 1, 2, 3 ]; $b = [ 2 => 3, 1 => 2, 0 => 1 ]; var_dump($a != $b); var_dump($a !== $b); a. true true b. true false c. false false d. false true
  13. 13. Quiz - 5 $a = [ 1, 2, 3 ]; $b = [ 2 => 3, 1 => 2, 0 => 1 ]; var_dump($a != $b); var_dump($a !== $b); a. true true b. true false c. false false d. false true
  14. 14. Quiz - 6 $a = [ 1, 2, 3 ]; $b = [ ‘a’ => 1, ‘b’ => 2, ‘c’ => 3 ]; var_dump($a == $b); a. true b. false
  15. 15. Quiz - 6 $a = [ 1, 2, 3 ]; $b = [ ‘a’ => 1, ‘b’ => 2, ‘c’ => 3 ]; var_dump($a == $b); a. true b. false == compare just the value and key regardless the order
  16. 16. Counting arrays count
  17. 17. Quiz - 7 $a = [ 1, 2, 3 ]; $b = []; $c = ‘Hello’; echo count($a, COUNT_NORMAL); echo count($b, COUNT_RECURSIVE); echo count($c, COUNT_RECURSIVE); ? ? ?
  18. 18. Quiz - 7 $a = [ 1, 2, 3 ]; $b = []; $c = ‘Hello’; echo count($a, COUNT_NORMAL); echo count($b, COUNT_RECURSIVE); echo count($c, COUNT_RECURSIVE); 3 0 1 With count() we cannot decides when a variable is a array indeed. Prefer to use is_array rather than count to decide when a variable is a array
  19. 19. Array keys isset vs array_key_exists vs in_array
  20. 20. Quiz - 8 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(isset($b[2])); var_dump(isset($a[3])); a. true false b. false true c. false false d. true true
  21. 21. Quiz - 8 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(isset($b[2])); var_dump(isset($a[3])); a. true false b. false true c. false false d. true true Prefer array_key_exists to determine if a key exists.
  22. 22. Quiz - 9 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(array_key_exists(2, $b)); var_dump(array_key_exists(3, $a)); a. true false b. false true c. false false d. true true
  23. 23. Quiz - 9 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 2 => NULL ]; var_dump(array_key_exists(2, $b)); var_dump(array_key_exists(3, $a)); a. true false b. false true c. false false d. true true
  24. 24. Quiz - 10 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(array_key_exists(2, $b)); var_dump(isset($a[2])); a. true false b. false true c. false false d. true true
  25. 25. Quiz - 10 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(array_key_exists(2, $b)); var_dump(isset($a[2])); a. true false b. false true c. false false d. true true
  26. 26. Quiz - 11 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(isset($b[2])); var_dump(in_array(3, $a)); a. true false b. false true c. false false d. true true
  27. 27. Quiz - 11 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(isset($b[2])); var_dump(in_array(3, $a)); a. true false b. false true c. false false d. true true
  28. 28. Quiz - 12 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(isset($b[2])); var_dump(in_array(3, $a)); var_dump(in_array(NULL, $a)) a. true false true b. false true false c. false false false d. true true true
  29. 29. Quiz - 12 $a = [ 0 => 1, 1=> 2, 2 => 3 ]; $b = [ 0 => 1, 3 => 2, 2 => NULL ]; var_dump(isset($b[2])); var_dump(in_array(3, $a)); var_dump(in_array(NULL, $a)) a. true false true b. false true false c. false false false d. true true true
  30. 30. Thank you ! @MatheusMarabesi github.com/marabesi

×