Top Banner
31

PHP - Basic introduction to arrays

Jul 16, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP - Basic introduction to arrays
Page 2: PHP - Basic introduction to arrays

Arrays basics

Arrays Enumeratives

vs

Arrays Associatives

Page 3: PHP - Basic introduction to arrays

Quiz - 1

$a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ];

$b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ];

a. $a is enumerative and $b is associativeb. $b is enumerative and $a is associativec. $a and $b are enumerativesd. $b and $a are associatives

Page 4: PHP - Basic introduction to arrays

Quiz - 1

$a = [ 0 => ‘A’, 1 => ‘B’, 2 => ‘C’ ];

$b = [ ‘A’ => 0, ‘B’ => 1, ‘C’ => 2 ];

a. $a is enumerative and $b is associativeb. $b is enumerative and $a is associativec. $a and $b are enumerativesd. $b and $a are associatives

Page 5: PHP - Basic introduction to arrays

Array operation

+

vs

array_merge

Page 6: PHP - Basic introduction to arrays

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 ]

Page 7: PHP - Basic introduction to arrays

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 ]

Page 8: PHP - Basic introduction to arrays

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 ]

Page 9: PHP - Basic introduction to arrays

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.

Page 10: PHP - Basic introduction to arrays

Comparing arrays

==vs

===

and

!=vs!==

Page 11: PHP - Basic introduction to arrays

Quiz - 4

$a = [ 1, 2, 3 ];

$b = [ 2 => 3, 1 => 2, 0 => 1 ];

var_dump($a == $b);var_dump($a === $b);

a. true trueb. true falsec. false falsed. false true

Page 12: PHP - Basic introduction to arrays

Quiz - 4

$a = [ 1, 2, 3 ];

$b = [ 2 => 3, 1 => 2, 0 => 1 ];

var_dump($a == $b);var_dump($a === $b);

a. true trueb. true falsec. false falsed. false true

== compare just the value and key regardless the order

=== compare the value, key and order

Page 13: PHP - Basic introduction to arrays

Quiz - 5

$a = [ 1, 2, 3 ];

$b = [ 2 => 3, 1 => 2, 0 => 1 ];

var_dump($a != $b);var_dump($a !== $b);

a. true trueb. true falsec. false falsed. false true

Page 14: PHP - Basic introduction to arrays

Quiz - 5

$a = [ 1, 2, 3 ];

$b = [ 2 => 3, 1 => 2, 0 => 1 ];

var_dump($a != $b);var_dump($a !== $b);

a. true trueb. true falsec. false falsed. false true

Page 15: PHP - Basic introduction to arrays

Quiz - 6

$a = [ 1, 2, 3 ];

$b = [ ‘a’ => 1, ‘b’ => 2, ‘c’ => 3 ];

var_dump($a == $b);

a. trueb. false

Page 16: PHP - Basic introduction to arrays

Quiz - 6

$a = [ 1, 2, 3 ];

$b = [ ‘a’ => 1, ‘b’ => 2, ‘c’ => 3 ];

var_dump($a == $b);

a. trueb. false

== compare just the value and key regardless the order

Page 17: PHP - Basic introduction to arrays

Counting arrays

count

Page 18: PHP - Basic introduction to arrays

Quiz - 7

$a = [ 1, 2, 3 ];$b = [];$c = ‘Hello’;

echo count($a, COUNT_NORMAL);echo count($b, COUNT_RECURSIVE);echo count($c, COUNT_RECURSIVE);

? ? ?

Page 19: PHP - Basic introduction to arrays

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

Page 20: PHP - Basic introduction to arrays

Array keys

isset

vs

array_key_exists

vs

in_array

Page 21: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 22: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Prefer array_key_exists to determine if a key exists.

Page 23: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 24: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 25: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 26: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 27: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 28: PHP - Basic introduction to arrays

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 falseb. false truec. false falsed. true true

Page 29: PHP - Basic introduction to arrays

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 trueb. false true falsec. false false falsed. true true true

Page 30: PHP - Basic introduction to arrays

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 trueb. false true falsec. false false falsed. true true true

Page 31: PHP - Basic introduction to arrays

Thank you !

@MatheusMarabesi

github.com/marabesi