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.

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

Copyright © 2002 Pearson Education, Inc. Slide 1.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
PHP Array Bayu Priyambadha, S.Kom. Array Array is collection of data that is saved in some place together and it is can accessed using index key 2 Kinds.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
PHP Using Arrays.
C SCI 116.  What does $animals contain? 2 $animals = "ostrich"; $animals = "anteater"; $animals = "orangutan"; $animals = "cheetah"; $animals = "hyena";
PHP Functions Composite Types. Functions  Declaration  function functionName(paramList) {  /* code goes here */ }  paramList is comma-separated list.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  Originally Personal Home Page  PHP is interpreted  PHP code is embedded.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)
CSE 341 Lecture 24 JavaScript arrays and objects slides created by Marty Stepp
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,
Strings and Arrays. String Is a sequence of characters. Example: “hello”, “how are you?”, “123”,and are all valid string values.
Strings IDIA 618 Spring 2013 Bridget M. Blodgett.
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
Arrays IDIA Spring 2012 Bridget M. Blodgett.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Lecture 7 Strings, Lists.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
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.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
Multidimensional Arrays CIT 336. The Basics Explaining Multidimensional Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
I Power Int 2 Computing Software Development High Level Language Constructs.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
CS320 Web and Internet Programming Introduction to PHP Chengyu Sun California State University, Los Angeles.
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.
PHP: A RRAYS, S TRINGS, AND F ILES CSCI 297 Scripting Languages - Day Three.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
1 Chapter 3 – Handling Strings and Arrays spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
ITM ITM 352 Array Processing. ITM Simple Array Processing  Arrays can be processed using iteration. Standard array-processing loop:  What.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Max(), with an array argument, returns the largest value in the array  With several numerical arguments, it returns the largest argument E.g., $arr =
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
NMD202 Web Scripting Week2. Web site
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.
Chungbuk HRDI of KCCI PhD Kang,Won-Chan PHP Programming (array)
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.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
String and Lists Dr. José M. Reyes Álamo.
Data types: Complex types (List)
Arrays in PHP are quite versatile
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Web Systems Development (CSC-215)
Web Systems Development (CSC-215)
String and Lists Dr. José M. Reyes Álamo.
PHP an introduction.
Presentation transcript:

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 by a conventional programming language. An element of an associative array can be accessed by a keyword. An element of an associative array can be accessed by a keyword. An associative array is like a dictionary or map. An associative array is like a dictionary or map. An array element can be of any type. An array element can be of any type. An array can be heterogeneous with its element types and structure. An array can be heterogeneous with its element types and structure. Many functions manipulating an array are provided. Many functions manipulating an array are provided.

2 PHP Array Indexed Array $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); echo "$animals[0]\n"; echo "$animals[2]\n"; echo "$animals\n"; dog fish Array

3 PHP Array Updating and Adding Elements $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); echo "$animals[1]\n"; $animals[1] = "tiger"; echo "$animals[1]\n"; $animals[] = "beaver"; echo "$animals[3]\n"; cat tiger beaver

4 PHP Array Associative Array $animals = array( "dog“ => 15,"cat“ = >8, "fish“ => 2); "dog“ => 15,"cat“ = >8, "fish“ => 2); echo "$animals[cat]\n"; echo "$animals[cat]\n"; $animals["bat"] = 100; $animals["bat"] = 100; echo "$animals[bat]\n"; echo "$animals[bat]\n"; 8 100

5 PHP Array Listing array element : for $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); for ($i = 0; $i < count($animals); $i++) { echo $i. "-th animal is a $animals[$i].\n"; echo $i. "-th animal is a $animals[$i].\n";} 0-th animal is a dog. 1-th animal is a cat. 2-th animal is a fish.

6 PHP Array Listing Array Elements: foreach $animals = $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); foreach ($animals as $animal) foreach ($animals as $animal) echo "$animal\n"; echo "$animal\n"; } dog cat fish

7 PHP Array while and each $animals = array( "dog“ => 15,"cat“ => 8, "fish“ => 2); "dog“ => 15,"cat“ => 8, "fish“ => 2); while ($item = each($animals)) print "weight of ". print "weight of ". $item["key"]. " is ". $item["key"]. " is ". $item["value"]. “.\n"; $item["value"]. “.\n"; weight of dog is 15. weight of cat is 8. weight of fish is 2.

8 PHP Array each and list $animals = array( "dog“ => 15, "cat“ => 8, "fish“ => 2); "dog“ => 15, "cat“ => 8, "fish“ => 2); while (list($key, $value) = each($animals)) print "weight of $key is $value.\n"; print "weight of $key is $value.\n"; weight of dog is 15. weight of cat is 8. weight of fish is 2.

9 PHP Array Multi-Dimensional Heterogeneous Array $books = array( array("title“ => “A", "author“ => “X"), "author“ => “X"), array("title“ => “B", “author“ => “Y", “author“ => “Y", “price“ => 25) “price“ => 25));print_r($books); Array ( [0] => Array ( [title] => A [author] => X ) [1] => Array ( [title] => B [author] => Y [price] => 25 )

10 PHP Array Nested Loops for ($i=0; $i < count($books); $i++) { print "$i-th book is:"; print "$i-th book is:"; while ( list($key, $value) = while ( list($key, $value) = each($books[$i]) ) each($books[$i]) ) print “ $key: $value"; print “ $key: $value"; print "\n"; print "\n";} 0-th book is: title: A author: X 1-th book is: title: B author: Y price: 25

11 PHP Array String as an Array $myString = "My chars"; echo "$myString\n"; echo "$myString[1]\n"; My chars y

12 PHP Array Array functions count(), sizeof() count(), sizeof() in_array() in_array() array_slice () array_slice () array_pad() array_pad() array_reverse() array_reverse() list( ) list( ) Sorting Functions Sorting Functions sort(), rsort() sort(), rsort() asort(), arsort() asort(), arsort() ksort(), krsort() ksort(), krsort()

13 PHP Array count(array1) and sizeof(array1) Returns the size of array array1. $animals = array ('dog', 'cat', 'fish'); echo count($animals); echo sizeof($animals); 3333

14 PHP Array array array_pad(array1, length, value) Pad array to length length with value. $scores = array(1, 2); array(1, 2); $padded = array_pad($scores, 5, 10); array_pad($scores, 5, 10);print_r($padded); Array ( [0] => 1 [1] => 2 [2] => 10 [3] => 10 [4] => 10 )

15 PHP Array array array_reverse(array1) Return an array with elements in reverse order. Array ( [0] => fish [1] => cat [2] => dog ) $animals = array('dog', 'cat', 'fish'); array('dog', 'cat', 'fish'); $reversed array_reverse($animals); print_r($reversed);

16 PHP Array array array_slice (array1, offset, length) Extract a slice of length from array1 starting at offset. $array1 = array(1, 2, 3, 4, 5, 6); array(1, 2, 3, 4, 5, 6); $subarray = array_slice(array1, 3, 2); array_slice(array1, 3, 2);print_r($subarray); Array ( [0] => 4 [1] => 5 )

17 PHP Array boolean in_array(value, array1) Check if value exists in array1. $animals = array('dog', 'cat', 'fish'); echo in_array('cat', $animals); echo in_array(‘monkey', $animals); 1 (true) (false)

18 PHP Array void list(var1, var2,...) The elements of an array are copied into the list of variables var1, var2,... $animals = array('dog', 'cat', 'fish'); list($a, $b, $c) = $animals; echo "$a, $b, $c"; dog, cat, fish

19 PHP Array sort(array) and rsort(array) Sort the elements in an array in increasing or decreasing order. $animals = array('dog', 'cat', fish'); array('dog', 'cat', fish');sort($animals);print_r($animals); Array ( [0] => cat [1] => dog [2] => fish )

20 PHP Array asort(array), arsort(array) Sort an array maintaining index association. $animals = array('dog', 'cat', 'fish'); array('dog', 'cat', 'fish');asort($animals);print_r($animals); Array ( [1] => cat [0] => dog [2] => fish )

21 PHP Array ksort(array), krsort(array) Sort array by keys. $animals = array('dog' => 15, array('dog' => 15, 'cat' => 8, 'fish' => 2); ksort($animals);print_r($animals); Array ( [cat] => 8 [dog] => 15 [fish] => 12 )