ITM 352 1 ITM 352 Array Processing. ITM 352 2 Simple Array Processing  Arrays can be processed using iteration. Standard array-processing loop:  What.

Slides:



Advertisements
Similar presentations
» PHP arrays are lists of values stored in key-value pairs. » Uses of arrays: Many built-in PHP environment variables. Database functions use arrays.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
PHP Programming with MySQL Slide 7-1 CHAPTER 7 Manipulating Arrays.
ITC 240: Web Application Programming
PHP (2) – Functions, Arrays, Databases, and sessions.
An Introduction to Programming with C++ Fifth Edition
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 7 Manipulating Arrays PHP Programming with MySQL.
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
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,
INTERNET APPLICATION DEVELOPMENT For More visit:
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
chap8 Chapter 8 Arrays (Hanly) chap8 2 Data Structure Simple data types use a simple memory to store a variable. Data Structure: a.
Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
Chapter 8 Arrays and Strings
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
ArrayList, Multidimensional Arrays
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Introduction to PHP – Part 2 Sudeshna Dey. Arrays A series of homogeneous elements Elements have values in form of stored data Has a key associated with.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
ITM © Port, Kazman1 ITM 352 Simple Arrays. ITM © Port, Kazman2 Arrays r Collections r Array basics m Declaration, allocation, and initialization.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
NMD202 Web Scripting Week2. Web site
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 PHP Array PHP Arrays An indexed array is similar to one provided by a conventional programming language. An indexed array is similar to one provided.
PHP WORKSHOP (Session 1) INFO 257 Supplement. Outline What is PHP ? PHP Language Basics PHP Exercise.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
PHP (Session 1) INFO 257 Supplement.
Chapter 6: Using Arrays.
Lecture 5 of Computer Science II
Computer Programming BCT 1113
Object Oriented Programming in java
ITM 352 Simple Arrays.
ITM 352 Array Processing.
Data Structures & Algorithms
Presentation transcript:

ITM ITM 352 Array Processing

ITM 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; } Do Lab #1

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';

ITM 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 "; Do Lab #2

ITM 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 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 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 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 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?

ITM 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