Arrays IDIA 618.185 Spring 2012 Bridget M. Blodgett.

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

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
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.
References and Data Structures. References Just as in C, you can create a variable that is a reference (or pointer) to another variable. That is, it contains.
CPS120: Introduction to Computer Science Searching and Sorting.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
ITC 240: Web Application Programming
PHP (2) – Functions, Arrays, Databases, and sessions.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Programming Logic and Design Fourth Edition, Comprehensive
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
INTERNET APPLICATION DEVELOPMENT For More visit:
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
The foreach LooptMyn1 The foreach Loop The foreach loop gives an easy way to iterate over arrays. foreach works only on arrays, and will issue an error.
Functions & Objects IDIA 618 Spring 2012 Bridget M. Blodgett.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
Lists in Python.
1 Arrays 2: Sorting and Searching Admin. §1) No class Thursday. §2) Will cover Strings next Tuesday. §3) Take in report. §4) Hand out program assignment.
Review IDIA 619 Spring 2013 Bridget M. Blodgett. HTML A basic HTML document looks like this: Sample page Sample page This is a simple sample. HTML user.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
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.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
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.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
1 CS428 Web Engineering Lecture 20 Control Structures, Loops and Pointers File Uploading Function (PHP - III)
What is PHP? IDIA Fall 2014 Bridget M Blodgett.
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.
Arrays. Indexed Arrays Use numbers as keys: $grades = array(); $grades[0] = ' A ' ; $grades[1] = ' A- ' ; $grades[2] = ' B+ ' ; Or with one statement:
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
PHP Arrays Dr. Charles Severance
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
I Power Higher Computing Software Development High Level Language Constructs.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
Chapter 3 Introduction to PHP. Incorporating PHP Within HTML By default, PHP documents end with the extension.php files ending with.htm or.html to also.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
PHP Arrays Dr. Charles Severance
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
CPS120: Introduction to Computer Science Sorting.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Creating PHP Pages Chapter 10 PHP Arrays. Arrays An array can store one or more values in a single variable name. An element of an associative accessed.
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.
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
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 Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Arrays: Checkboxes and Textareas
PHP Arrays Dr. Charles Severance
JavaScript: Control Statements.
While Loops BIS1523 – Lecture 12.
Web Systems Development (CSC-215)
Data Structures – 1D Lists
Iteration: Beyond the Basic PERFORM
Basics (Cont...).
Programming Logic and Design Fifth Edition, Comprehensive
LOOPING STRUCTURE Chapter - 7 Padasalai
PHP Array.
Presentation transcript:

Arrays IDIA Spring 2012 Bridget M. Blodgett

Arrays Method of storing larger amounts of information that is related in some way e.g. The names of your intramural team $team = array('Bill', 'Joe', 'Mike', 'Chris', 'Jim'); Arrays begin counting at 0 and go up 2

Metaphors For Arrays Matchboxes Taped Together Books inside a cardboard box Shelves of boxes inside a warehouse at Amazon

Types of Arrays Up til now we’ve only dealt with numerically indexed arrays – These arrays receive a index number which is associated with a particular piece of content within the array Associative arrays allow the index to be a keyword instead of a number

Numeric Arrays When creating a numeric array the index number associated with values of each “box” can be defined automatically or explicitly – $paper[] = “Copier” is an automatically indexed – $paper[4] = “Photo” is explicitly indexed The print_r() function will allow you to print the contents of the array (including both the index and the value) – Alternatively a for loop may be used

Associative Arrays Associative arrays use keywords as index items, making it easier to adjust or remember the location of information in the array $paper['copier'] = "Copier & Multipurpose"; $paper['inkjet'] = "Inkjet Printer"; $paper['laser'] = "Laser Printer"; $paper['photo'] = "Photographic Paper"; This allows each “box” of the array to convey useful information to the programmer or anyone else looking at the PHP code

array Keyword Instead of assigning each value of an array individually, it is often faster to employ the array() function to simply create an array out of a series of values <?php $p1 = array("Copier", "Inkjet", "Laser", "Photo"); echo "p1 element: ". $p1[2]. " "; $p2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); echo "p2 element: ". $p2['inkjet']. " "; ?>

Write an array variable of weather conditions with the following values: rain, sunshine, clouds, hail, sleet, snow, wind. Using the array variable for all the weather conditions, echo the following statement to the browser: “We've seen all kinds of weather this month. At the beginning of the month, we had snow and wind. Then came sunshine with a few clouds and some rain. At least we didn't get any hail or sleet.”

foreach…as Loop This loop is only for use with arrays It allows you to go through each box in an array and do something to the value of that box – This will continue until the last box is reached before automatically exiting – Examples 6-6 & 6-7 An alternative is to use the list() and each() functions – Example 6-8 – Allows you to use a different looping system

Multidimensional Arrays Multidimensional arrays are arrays that contain other arrays within each box – Design and logic limitations usually means that arrays are nested no more than 3 deep It can be useful to classify data into related categories – Example 6-10 Foreach…as loops may also be nested to allow the different levels of the array to be accessed

Additional Array Functions is_array() checks to make sure the selected variable is an array count() counts all the elements of the top level array. count($array,1) would also include all sub-arrays reset() moves the pointer of the current foreach loop or each() function back to the first item of the array end() moves the pointer of the current foreach loop of each() function to the last item of the array

sort() & shuffle() sort() allows you to sort the items within the array according to their type – Numeric values are sorted lowest to highest – Alphabetic values are sorted a->z on the first letter shuffle() puts the elements of an array in a random order Both functions act directly upon the array, changing the order or its values permanently

explode() & compact() explode() takes a string with a common separator and puts each word into one index of an array – Example 6-12 compact() takes several variables and assigns them and their values to an array – Compact requires an array of variable names so use ‘’ instead of $ compact() and explode() can be used together to extract all the words in several strings and assign it all to an array

extract() extract() takes the key/value pairs from an array and turns them into variables – Very useful for taking information from the $_GET and $_POST variables If the array key is the same as an existing variable it will overwrite the existing variable! Additional parameters help avoid this – extract($_GET, EXTR_PREFIX_ALL, 'fromget');