itm 352 1 itm 352 array processing. itm 352 2 simple array processing arrays can be processed using...
DESCRIPTION
ITM Multi-dimensional Arrays Arrays can hold any PHP data type including other arrays This is useful for creating multi-dimensional arrays $row1 = array('a','b','c'); $row2 = array('d','e','f'); $row3 = array('g','h','i'); // make 2-dim array of rows $matrix = array($row1, $row2, $row3); echo $matrix[0][0]; echo $matrix[1][2]; echo $matrix[2][1]; $matrix[2][2] = 'I';TRANSCRIPT
ITM 352 1
ITM 352
Array Processing
ITM 352 2
Simple Array ProcessingArrays can be processed using iteration.
Standard array-processing loop:
What about associative arrays?
for ($i=0; $i < count($angles); $i++) { print "angle $i: ". $angles[$i];}
foreach ($products as $key => $value) { print 'key ' . $key . ' has value ' $value;}
Do Lab #1
ITM 352 3
Multi-dimensional Arrays Arrays can hold any PHP data type including other
arrays This is useful for creating multi-dimensional
arrays$row1 = array('a','b','c');$row2 = array('d','e','f');$row3 = array('g','h','i');// make 2-dim array of rows$matrix = array($row1, $row2, $row3);
echo $matrix[0][0];echo $matrix[1][2];echo $matrix[2][1];$matrix[2][2] = 'I';
ITM 352 4
Multi-dimensional Arrays (cont.)There's no reason you cannot mix indexed
and associative arrays (you will find this useful later)
$product1 = array('name' => 'small gumball', 'price' => 0.02);$product2 = array('name' => 'medium gumball', 'price' => 0.05);$product3 = array('name' => 'large gumball', 'price' => 0.07);
// array of all products$products = array($product1, $product2, $product3);
for($i=0; $i<count($products); $i++) echo "Product: $i is {$products[$i]['name']} and costs {$products[$i]['price']} each<br>";
Do Lab #2
ITM 352 5
Element Existence Sometimes it's useful to know an element exists in
an array. Use the array_key_exists() function for this:$product = array('name' => 'small gumball', 'filling' =>
NULL, 'price' => 0.04);if( array_key_exists('name', $product) )
echo "Product is {$product['name']}";
Why not just use isset()?array_key_exists('filling', $product) // returns?
isset($product['filling']) // returns?
Do Lab #3
ITM 352 6
Searching
You can test for an element in array with in_array()
if (in_array('small gumball', $product))print('we sell small gumballs');
You can search for an element key by value in an array with array_search()
print "the key for small gumball is " . array_search('small gumball', $product);
Do Lab #4
ITM 352 7
Sorting
There are many ways to sort the elements in array such as sort(),asort(),ksort():
$a = array('3', 'a', 'c', 'b', '2', '1');sort($a);
See LP, p. 60 or use the on-line manual at php.net Take care with sorting functions as they do not return
the sorted array. The array is directly manipulated (the parameter is passed by reference).
ITM 352 8
Keys and Values$product = array('name' => 'small gumball', 'filling'
=> 'solid', 'price' => 0.04);
Use array_keys() to get an array of an array's keys
$prodKeys = array_keys($product);
Use array_values() to get an array of an array's values
$prodValues = array_values($product);
When would you want to use either of these?
ITM 352 9
Removing and Inserting Elements$product = array('small gumball','solid', 0.04);
Usually you will simply create a new array if you need to insert or remove elements
$prodNoFilling = array($product[0], $product[2]);
You can use array_splice() to insert or remove elements directly
array_splice($product, 1, 1); // removes elem 1array_splice($product, 1, 1, 'hollow'); // replaces elem 1array_splice($product, 1, 0, 'logo'); // inserts at elem
1
Challenge: What happens to $product if you executed the above as is?
ITM 352 10
Vector Functions$nums1 = array(1,2,3,4,5, 5.0);$nums2 = array(5,6,7,8,9); Merging arrays – splices together$mergeNums = array_merge($nums1, $nums2); Array Unique – all unique values (uses ==)$unionNums = array_unique($nums1); Array Intersection – all common values (uses ==)$intsctNums = array_intersect($nums1, $nums2); Array Difference – return all values of $nums1 that are
not in $nums2$diffNums = array_diff($nums1, $nums2);
Do Lab #5