$value) { print 'key ' . $key . ' has value ' $value; }"> $value) { print 'key ' . $key . ' has value ' $value; }">
Download presentation
Presentation is loading. Please wait.
1
ITM 352 Array Processing
2
Simple Array Processing
Arrays 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; }
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';
4
Multi-dimensional Arrays (cont.)
You can 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); // Create array of all products $products = array($product1, $product2, $product3); // Now print the array of products for ($i=0; $i<count($products); $i++) { echo "Product: $i is {$products[$i]['name']} and costs {$products[$i]['price']} each<BR>"; }
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?
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);
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 the on-line manual at php.net for help with functions Take care with sorting functions as they do not return the sorted array. The array is directly manipulated (the parameter is passed by reference).
8
Keys and Values Use array_keys() to get an array of an array's keys
$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?
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 1 array_splice($product, 1, 1, 'hollow'); // replaces elem 1 array_splice($product, 1, 0, 'logo'); // inserts at elem 1 Challenge: What happens to $product if you executed the above as is?
10
Vector Functions Merging arrays – splices together
$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);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.