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,

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

Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Arrays.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
ITC 240: Web Application Programming
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Java Unit 9: Arrays Declaring and Processing Arrays.
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
INTERNET APPLICATION DEVELOPMENT For More visit:
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.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
The University of Texas – Pan American
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
NUMERIC ARRAYS IN PHP BY SKYLAR NEILSON. WHAT ARE NUMERIC ARRAYS? They are arrays with a numeric index and the values are stored and accessed in linear.
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.
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Arrays “Array, I'm bound array, 'cross the wide Missouri.” ‒ Old Programmer’s Folk Song © Copyright 2014, Fred McClurg All Rights Reserved.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Built-in Data Structures in Python An Introduction.
LISTS AND ARRAYS CHAPTER Topics  C# Collections  List –Flexible collection of variable length  Array –Standard arrays  Multidimensional Arrays.
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays Dr. Jose Annunziato. Arrays Up to this point we have been working with individual primitive data types Arrays allow working with multiple instances.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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.
PHP Overview. What is PHP Widely available scripting language Free Alternative to Microsoft’s ASP Runs on the Web Server; not in the browser Example:
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 6: Using Arrays.
The University of Texas – Pan American
Arrays, For loop While loop Do while loop
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
JavaScript Arrays.
Multidimensional array
Class code for pythonroom.com cchsp2cs
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

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, beginning at 0. Indexed arrays are used when you identify things by their position. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value.

Creating Arrays 1.) Integer-Indexed Array Numerically indexed arrays can be created to start at any index value There are two ways in creating Array in PHP: formal and informal array declaration methodologies.

Creating Arrays 1.) Integer-Indexed Array A.) Informal array declaration Individual elements of a PHP array are referenced by denoting the element between a pair of square brackets, like this: $state[0] = “Norton"; $state[1] = “NUM"; $state[2] = “RUPP";... $state[49] = “IFL"; You can then display the first element of the array $state like this: echo $state[0];

1.) Integer-Indexed Array B.) Formal array declaration PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use: $numbers = array(5, 4, 3, 2, 1); $words = array("Web", "Database", "Applications"); // Print the third element from the array // of integers: 3 echo $numbers[2]; // Print the first element from the array // of strings: "Web“ echo $words[0]; Creating Arrays

2.) Associative Array A.) Informal array declaration Individual elements of a PHP array are referenced by denoting the element between a pair of square brackets, like this: $state["Delaware"] = "December 7, 1787"; $state["Pennsylvania"] = "December 12, 1787"; $state["New Jersey"] = "December 18, 1787";... $state["Hawaii"] = "August 21, 1959"; You can then display the first element of the array $state like this: echo $state["Delaware"] ;

1.) Associative Array B.) Formal array declaration PHP provides the array( ) language construct that creates arrays. The following examples show how arrays of integers and strings can be constructed and assigned to variables for later use: $array = array("first"=>1, "second"=>2, "third"=>3); // Echo out the second element: prints "2“ echo $array["second"]; $languages = array ("Spain" => "Spanish", "Ireland" => "Gaelic", "United States" => "English"); Creating Arrays

Heterogeneous arrays The values that can be stored in a single PHP array don't have to be of the same type. PHP arrays can contain heterogeneous values $mixedBag = array("cat", 42, 8.5, false); var_dump($mixedBag); Print_r($mixedBag); // var_dump output: array(4) { [0]=> string(3) "cat" [1]=> int(42) [2]=> float(8.5) [3]=> bool(false) } // print_r output: Array ( [0] => cat [1] => 42 [2] => 8.5 [3] => )

Multidimensional arrays PHP arrays can also hold other arrays creating multidimensional arrays. Multi-dimensional arrays A two dimensional array <?php // A two dimensional array using integer indexes $planets = array(array("Mercury", 0.39, 0.38), array("Venus", 0.72, 0.95), array("Earth", 1.0, 1.0), array("Mars", 1.52, 0.53) ); // prints "Earth" print $planets[2][0] ?>

Using foreach Loops with Arrays the easiest way to iterate through—or traverse—an array is using the foreach statement The foreach statement has two forms: foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement The first form assigns the value from the element to a variable identified with the as keyword; the second form assigns both the key and the value to a pair of variables.

Example of the first form <?php // Construct an array of integers $lengths = array(0, 107, 202, 400, 475); // Convert an array of centimeter lengths to inches foreach($lengths as $cm){ $inch = $cm / 2.54; echo "$cm centimeters = $inch inches\n"; } ?> The example iterates through the array in the same order it was created: 0 centimeters = 0 inches 107 centimeters = inches 202 centimeters = inches 400 centimeters = inches 475 centimeters = inches

Example of the second form <?php $sounds = array("cow"=>"moo", "dog"=>"woof", "pig"=>"oink", "duck"=>"quack"); foreach ($sounds as $animal => $sound) { echo " Old MacDonald had a farm EIEIO"; echo " And on that farm he had a $animal EIEIO"; echo " With a $sound-$sound here"; echo " And a $sound-$sound there"; echo " Here a $sound, there a $sound"; echo " Everywhere a $sound-$sound"; } ?>

Some Useful Functions current( ): returns the value pointed to by the array's internal index. each( ): returns an array that holds the index key and the value of the current element. each( ) returns has four elements: two that hold the key, accessed by the numeric index 0 and the associative key key; and two that hold the value, accessed by the numeric index 1 and the associative key value. end( ): returns the value in last index next( ): returns the value in next index prev( ): returns the value in previous index reset( ): returns array's internal pointer to the first element key( ): returns the key in current index

Some Useful Functions(cont) The list( ) function isn't really a function, but a language construct that assigns multiple variables from an array expression: list($var1, $var2,...) = array_expression <?php $sounds = array ("pig"=>"ouk", "cow"=>"moo", "duck"=>"quack", "dog"=>"woof"); while (list($animal, $sound) = each($sounds)){ echo " Old MacDonald had a farm EIEIO"; echo " And on that farm he had a $animal EIEIO"; echo " With a $sound-$sound here"; echo " And a $sound-$sound there"; echo " Here a $sound, there a $sound"; echo " Everywhere a $sound-$sound"; } ?>

Basic Array Functions Counting elements in Array integer count(mixed var) Finding maximun and minimum values in Array number max(array numbers) number min(array numbers) Finding values in array boolean in_array(mixed needle, array haystack [, boolean strict]) The in_array( ) function returns true if an array haystack contains a specific value needle: $smallPrimes = array(3, 5, 7, 11, 13, 17, 19, 23, 29); if (in_array(19, $smallPrimes)) echo "19 is a small prime number"; // Always printed

Basic Array Functions(Cont.) Finding values in array mixed array_search(mixed needle, array haystack [, boolean strict]) The array_search( ) function works the same way as the in_array( ) function, except the key of the matching value needle is returned rather than the Boolean value true: $measure = array("inch"=>1, "foot"=>12, "yard"=>36); // prints "foot“ echo array_search(12, $measure); Reordering elements in array array array_reverse(array source [, bool preserve_keys]) $count = array("zero", "one", "two", "three"); $countdown = array_reverse($count); //setting the second optional argument. count = array("zero", "one", "two", "three"); $countdown = array_reverse($count, true); print_r($countdown);

Basic Array Functions(Cont.) Sorting Integer-Indexed Arrays on values sort(array subject [, integer sort_flag]) rsort(array subject [, integer sort_flag]) $numbers = array(24, 19, 3, 16, 56, 8, 171); sort($numbers); foreach($numbers as $n) echo $n. " "; $numbers = array(24, 19, 3, 16, 56, 8, 171); rsort($numbers); foreach($numbers as $n) echo $n. " ";

Basic Array Functions(Cont.) Sorting Associative Arrays on values asort(array subject [, integer sort_flag]) arsort(array subject [, integer sort_flag]) $map = array("o"=>"kk", "e"=>"zz", "z"=>"hh", "a"=>"rr"); asort($map);print_r($map); Sorting on keys integer ksort(array subject [, integer sort_flag]) integer krsort(array subject [, integer sort_flag])

The end of Chapter 2 Thanks for your paying attention