need to manage multiple pieces of data as a group of things rather than individual values. ❒ A collection is a compound data type that contains multiple values to do just this. ❒ We have already seen a kind of collection: strings (a collection of characters). ❍ the operation $aString[5] returns the 6th character (counting from zero).
Association ❒ Strings could be used to create another type of collection, lists (like a list you might create on paper for groceries). ❍ $groceries = “Item 1: Eggs, Item 2: Milk, Item 3: Spam" ❒ Both are ordered but the particular sequence may or may not be important. ❍ Strings: sequence of characters. Order is important ❍ Grocery List: collection of items. Order is unimportant ❍ Task List: sequence of tasks. Order is important
Association (cont.) ❒ When a collection must have a particular sequence, its values are indexed by position number ❍ Strings: $name = "Fred", $name[0] = 'F', $name[2] = 'e' ❒ When a collection doesn't have a particular sequence, its values are associated by keys ❍ Grocery List: Value for key 'Item 3' is the value 'Spam'
Variables? $product1 = "Phone"; $product2 = "Candy"; $product3 = "Soda"; for($i=1; $i<=3; $i++) { $ident = "product$i"; print $$ident; } ❒ This is sort of a collection, but not terribly convenient or efficient. Why?
kind of ordered collection. ❒ Special features: ❍ Built-in to PHP, has special syntax. ❍ Elements can be any type of data (including other arrays!) ❍ Collection can change size anytime ❍ Arrays may indexed or associative (or both!) ❒ Note: All arrays in PHP are associative ❍ Keys are automatically set to integers if you don’t specify them
An array is a named collection of values value 17 'Hi' 9.33 NULL true 0 1 2 3 4 element position (identified by index) $indexedArray value 17 'Hi' 9.33 NULL true 'age' 'greet' 'amount' 'notset' 'is_easy' $associativeArray element position (identified by key) Do Lab Exercise 1
$product['price'] $product['price'] $product['price'] $product['price'] = 32; Array name key - also called a subscript - must be an int, - or an expression that evaluates to an int keyed variable - also called an element or subscripted variable Note that "element" may refer to either a single indexed variable in the array or the value of a single indexed variable. Value of the indexed variable; also called an element of the array
(indexed) $temperature[$n + 2] $temperature[$n + 2] $temperature[$n + 2] $temperature[$n + 2] = 32; Array name Index - also called a subscript - must be an int, - or an expression that evaluates to an int Indexed variable - also called an element or subscripted variable Note that "element" may refer to either a single indexed variable in the array or the value of a single indexed variable. Value of the indexed variable; also called an element of the array
of arrays: ability to access each element easily. Use notation to access elements of an array. ❍ If the key-expression has integer value n, this returns the n'th element in array-name, counting from zero (!) ❍ If the key-expression is a string, it returns the value associated with that key array-name[key-expression]
= 75; echo $grades[0]; $i = 2; $clocks[$i-1] = time(); $clocks[] = time()+1; printf("<BR>time[1] is %s and time[2] is %s", $clocks[$i-1], $clocks[2]); $name['first'] = "Rumple"; $name['last'] = "Stilskin"; echo "<BR>name is {$name['first']} {$name['last']}"; $thingy[] = 1; $thingy[] = 4; echo "<BR> $thingy[0] and $thingy[1]"; This is how you add elements to an existing indexed array.
Using a subscript larger than length-1 or less than 0 or a key that does not exist will not cause the program to stop executing. ❍ No element will be returned ❍ Take care to use the correct subscripts! ❒ Be careful of interpolation problems when accessing array elements within a string echo "The product is $product['name']"; // won't work! echo "The product is $product[name]"; echo "The product is {$product['name']}"; Do Lab Exercise 2
arrays ❒ One method: initialization list using array(). ❒ This allocates the array as a 3-element array and initializes its elements the same as: $angles[0] = 3.5; $angles[1] = 9.7; $angles[2] = 15.01; ❒ Key-value pairs can also be used $angles = array(3.5, 9.7, 15.01); $product = array('name' => 'gumball', 'price' => 0.07, 'quantity' => 1500); Do Lab Exercise 3
of an Array ❒ Size of $anArray can be determined using count($anArray) or sizeof($anArray). ❒ This is useful when using an array of unknown size in a loop: $counter=0; while( $counter < sizeof($anArray) ) { echo "element $counter is {$anArray[$counter]}"; $counter++; } How big is $anArray? We don’t need to know! Just use count($anArray)or sizeof($anArray). Do Lab Exercise 4