Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and...

19
Chapter 3 Array

Transcript of Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and...

Page 1: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Chapter 3

Array

Page 2: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Indexed Versus Associative Arrays

There are two kinds of arrays in PHP: indexed and associative.

The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position.

Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.

Page 3: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Creating Arrays

1.) Integer-Indexed Array

Numerically indexed arrays can be created to start at any index value

There are two ways in creating Array in PHP: formal and informal array declaration methodologies.

Page 4: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Creating Arrays1.) Integer-Indexed Array

A.) Informal array declaration Individual elements of a PHP array are referenced by denoting the element between a pair of square brackets, like this:

$state[0] = “Norton";

$state[1] = “NUM";$state[2] = “RUPP";...$state[49] = “IFL";

You can then display the first element of the array $state like this:echo $state[0];

Page 5: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

1.) Integer-Indexed ArrayB.) Formal array declaration

PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use:

$numbers = array(5, 4, 3, 2, 1);

$words = array("Web", "Database", "Applications");

// Print the third element from the array

// of integers: 3

echo $numbers[2];

// Print the first element from the array

// of strings: "Web“

echo $words[0];

Creating Arrays

Page 6: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Creating Arrays2.) Associative Array

A.) Informal array declaration

Individual elements of a PHP array are referenced by

denoting the element between a pair of square brackets, like

this:

$state["Delaware"] = "December 7, 1787";

$state["Pennsylvania"] = "December 12, 1787";

$state["New Jersey"] = "December 18, 1787";

...

$state["Hawaii"] = "August 21, 1959";

You can then display the first element of the array $state like

this:

echo $state["Delaware"] ;

Page 7: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

1.) Associative ArrayB.) Formal array declaration

PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use:

$array = array("first"=>1, "second"=>2, "third"=>3); // Echo out the second element: prints "2“echo $array["second"]; $languages = array ("Spain" => "Spanish",

"Ireland" => "Gaelic","United States" =>

"English");

Creating Arrays

Page 8: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Heterogeneous arrays

The values that can be stored in a single PHP array don't have to be of the same type.

PHP arrays can contain heterogeneous values $mixedBag = array("cat", 42, 8.5, false);

var_dump($mixedBag);

Print_r($mixedBag);

// var_dump output:

array(4) { [0]=> string(3) "cat" [1]=> int(42) [2]=> float(8.5) [3]=> bool(false) }

// print_r output:

Array ( [0] => cat [1] => 42 [2] => 8.5 [3] => )

Page 9: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Multidimensional arraysPHP arrays can also hold other arrays creating multidimensional arrays.

<html><head> <title>Multi-dimensional arrays</title></head><body bgcolor="#ffffff"><h2>A two dimensional array</h2><?php // A two dimensional array using integer indexes $planets = array(array("Mercury", 0.39, 0.38),

array("Venus", 0.72, 0.95), array("Earth", 1.0, 1.0), array("Mars", 1.52, 0.53) );

// prints "Earth" print $planets[2][0]?>

Page 10: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Using foreach Loops with Arrays

the easiest way to iterate through—or traverse—an array is using the foreach statement

The foreach statement has two forms:

foreach(array_expression as $value) statement

foreach(array_expression as $key => $value) statement

The first form assigns the value from the element to a variable identified with the as keyword;

the second form assigns both the key and the value to a pair of variables.

Page 11: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Example of the first form<?php

// Construct an array of integers$lengths = array(0, 107, 202, 400, 475);// Convert an array of centimeter lengths to inchesforeach($lengths as $cm){ $inch = $cm / 2.54; echo "$cm centimeters = $inch inches\n";}

?>The example iterates through the array in the same order it was created:

0 centimeters = 0 inches 107 centimeters = 42.125984251969 inches202 centimeters = 79.527559055118 inches400 centimeters = 157.48031496063 inches475 centimeters = 193.87755102041 inches

Page 12: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Example of the second form

<?php$sounds = array("cow"=>"moo", "dog"=>"woof",

"pig"=>"oink", "duck"=>"quack");

foreach ($sounds as $animal => $sound)

{

echo "<p>Old MacDonald had a farm EIEIO";

echo "<br>And on that farm he had a $animal EIEIO";

echo "<br>With a $sound-$sound here";

echo "<br>And a $sound-$sound there";

echo "<br>Here a $sound, there a $sound";

echo "<br>Everywhere a $sound-$sound";

}

?>

Page 13: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Some Useful Functionscurrent( ): returns the value pointed to by the array's internal index.

each( ): returns an array that holds the index key and the value of the current element.

each( ) returns has four elements: two that hold the key, accessed by the numeric index 0 and the associative key key; and two that hold the value, accessed by the numeric index 1 and the associative key value.

end( ): returns the value in last index

next( ): returns the value in next index

prev( ): returns the value in previous index

reset( ): returns array's internal pointer to the first element

key( ): returns the key in current index

Page 14: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Some Useful Functions(cont)The list( ) function isn't really a function, but a language construct that assigns multiple variables from an array expression: list($var1, $var2, ...) = array_expression <?php

$sounds = array ("pig"=>"ouk", "cow"=>"moo", "duck"=>"quack",

"dog"=>"woof");while (list($animal, $sound) = each($sounds)){ echo "<p>Old MacDonald had a farm EIEIO"; echo "<br>And on that farm he had a $animal EIEIO"; echo "<br>With a $sound-$sound here"; echo "<br>And a $sound-$sound there"; echo "<br>Here a $sound, there a $sound"; echo "<br>Everywhere a $sound-$sound";

}

?>

Page 15: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Basic Array Functions Counting elements in Arrayinteger count(mixed var)

Finding maximun and minimum values in Arraynumber max(array numbers)number min(array numbers)

Finding values in arrayboolean in_array(mixed needle, array haystack [, boolean strict])

The in_array( ) function returns true if an array haystack contains a specific value needle: $smallPrimes = array(3, 5, 7, 11, 13, 17, 19, 23, 29);if (in_array(19, $smallPrimes)) echo "19 is a small prime number"; // Always printed

Page 16: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Basic Array Functions(Cont.) Finding values in array

mixed array_search(mixed needle, array haystack [, boolean strict]) The array_search( ) function works the same way as the in_array( ) function, except the key of the matching value needle is returned rather than the Boolean value true:

$measure = array("inch"=>1, "foot"=>12, "yard"=>36);// prints "foot“echo array_search(12, $measure);

Reordering elements in arrayarray array_reverse(array source [, bool preserve_keys])

$count = array("zero", "one", "two", "three");$countdown = array_reverse($count); //setting the second optional argument.count = array("zero", "one", "two", "three");$countdown = array_reverse($count, true);print_r($countdown);

Page 17: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Basic Array Functions(Cont.) Sorting Integer-Indexed Arrays on valuessort(array subject [, integer sort_flag])rsort(array subject [, integer sort_flag])

$numbers = array(24, 19, 3, 16, 56, 8, 171);sort($numbers);foreach($numbers as $n)

echo $n . " ";

$numbers = array(24, 19, 3, 16, 56, 8, 171);rsort($numbers);foreach($numbers as $n)

echo $n . " ";

Page 18: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

Basic Array Functions(Cont.) Sorting Associative Arrays on valuesasort(array subject [, integer sort_flag])

arsort(array subject [, integer sort_flag])

$map = array("o"=>"kk", "e"=>"zz", "z"=>"hh", "a"=>"rr");

asort($map);print_r($map);

Sorting on keys

integer ksort(array subject [, integer sort_flag])

integer krsort(array subject [, integer sort_flag])

Page 19: Chapter 3 Array. Indexed Versus Associative Arrays There are two kinds of arrays in PHP: indexed and associative. The keys of an indexed array are integers,

The end of Chapter 2

Thanks for your paying attention