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.

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

IntroductION to SERVER-SIDE WEB PROGRAMMING
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.
JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
Creating PHP Pages Chapter 7 PHP Decisions Making.
1 Arrays in PHP PHP Functions. 2 Array Definition An array is a complex variable that enables you to store multiple values in a single variable; It comes.
PHP Using Arrays.
Functions & Arrays. Functions Functions offer the ability for programmers to group together program code that performs specific task or function into.
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.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
1.6 Functions. A relation is a pairing of input values with output values. It can be shown as a set of ordered pairs (x,y), where x is an input and y.
1 CS428 Web Engineering Lecture 19 Data Types (PHP - II)
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
PHP – Get & Post; Functions; and Arrays IS6116 – 07 th February 2011.
Using Arrays in PHP (Part I) Lecture # 6. What is an array? It is a collection of multiple values assembled into one variable An array can consist of.
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,
Class 3Intro to Databases Arrays Sending Values to a Script Manually For and While Loops.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKPRO GUIDE Chapter 2: Programming with PHP Copyright © 2012 by Larry Ullman Dr. Mogeeb Mosleh Saturday ( pm)
Arrays IDIA Spring 2012 Bridget M. Blodgett.
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.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Apache, MySQL and PHP Installation and Configuration Chapter 4 XAMPP Installation and Configuration.
Apache, MySQL and PHP Installation and Configuration Chapter 3 PHP Installation and Configuration.
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.
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 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.
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.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
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.
Apache, MySQL and PHP Installation and Configuration Chapter 1 Apache Installation and Configuration.
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.
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.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Apache, MySQL and PHP Installation and Configuration Chapter 2 MySQL Installation and Configuration.
Creating PHP Pages Chapter 6 PHP Variables, Constants and Operators.
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.
Creating PHP Pages Chapter 5 PHP Structure and Syntax.
Max(), with an array argument, returns the largest value in the array  With several numerical arguments, it returns the largest argument E.g., $arr =
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
NMD202 Web Scripting Week2. Web site
1 februari 2016 PHP array. 1 februari 2010 Titel van de presentatie 2 Een array is een verzameling van waarden // zonder waarden initieren $salaries =
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,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
Magic 8 ball. "Design and write a python program that emulates a Magic Eight Ball. Your program should continually prompt the user to enter a question.
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.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Working with Collections of Data
Arrays: Checkboxes and Textareas
Arrays in PHP are quite versatile
PHP Arrays Functions.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Web Programming– UFCFB Lecture 19-20
While Loops BIS1523 – Lecture 12.
Web Systems Development (CSC-215)
Web Programming Language
Web Programming– UFCFB Lecture 21
PHP PART 2.
PHP an introduction.
Presentation transcript:

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

Arrays Array Types : Indexed and Associative Arrays, based on whether the key is numeric or string. In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. PHP.net says: "There are no different indexed and associative array types in PHP; there is only one array type, which can both contain integer and string indices.

Keyword An array key can be thought of as the address or reference to a specific value contained within an array. A key can be an integer or a string Floats used as keys are truncated to integers If you don't specify a key, the array key will automatically start with 0 (zero) and auto increment by 1 each time a new value is entered into the array. Key must be unique.

Creating an Array An array can be created by the array() language-construct. It takes a number of comma-separated key => value pairs. Example $person = array(‘manager'=>‘rico', ‘supervisor'=>‘john', ‘staff'=>‘mike')

Array Values Array values can be accessed a number of ways. Most directly, arrays can be output using print() and echo. However, you have to indicate the key of the value you wish to print, otherwise the output will just be "Array".

Array Example Create an array with numeric keys, automatically created starting at zero: $colors = array('red', 'green', 'blue') echo $colors[1]; //green echo $colors; //Array echo $colors[0]; //red Create an array with numeric with keys starting at 1, then incremented automatically by 1: $colors = array(1=>'red', 'green', 'blue') echo $colors[1]; //red echo $colors[3]; //blue echo $colors[0]; //Notice: Undefined

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

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

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

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

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

View Array Structure You can quickly view the keys and values of an array using print_r() : $colors = array('red', 'green', 'blue'); print " "; print_r($colors); print " "; Output : Array ( [0] => red [1] => green [2] => blue )

View Array Structure View the structure, keys and values of an array using var_dump() : $colors = array('red', 'green', 'blue'); print " "; var_dump($colors); print " "; Output : array(3) { [0]=> string(3) "red" [1]=> string(5) "green" [2]=> string(4) "blue" }

Delete Array Element After an array has been created, you can add values to the 'end' of the array: unset($colors[2]); print " "; print_r($colors); print " "; Output : Array ( [0] => red [1] => green [3] => orange )

Merge Array Combine arrays : $colors = array('red', 'green', 'blue'); $others = array('purple', 'pink'); $all = array_merge($colors,$others); Output : Array ([0] => red [1] => green [2] => blue [3] => purple [4] => pink )

Count and Delete or Reset Array You can count the number of values in an array : $numColors = count ($colors); Delete an array : unset($colors); Reset an array (delete all keys and values but keep array) : $colors = array();

Transform Take the colors and transform them into a string with at the end of each color : $newString = implode (' ',$colors); Take the colors and transform them into a string with at the end of each color: $names = "Bob, Terry, Carl"; $guysArray = explode(', ', $names); print " "; print_r($guysArray); print " "; Output : Array ( [0] => Bob [1] => Terry [2] => Carl )

Sorting Arrays Arrays can be sorted by key or value You can sort and keep keys aligned, or sort values and have new keys assigned. Using sort () function, sort() is alphabetical if values are strings, or numeric if values are numeric. Example: $colors = array('red', 'green', 'blue'); sort($colors); print_r($colors); Output : Array ( [0] => blue [1] => green [2] => red )

Sorting Arrays rsort() Function rsort() sorts in reverse : Example: $colors = array('green', 'red', 'blue'); rsort($colors); print_r($colors); Output : Array ( [0] => red [1] => green [2] => blue )

Sorting Arrays ksort() Function ksort() keys, keeping values correlated to keys : Example: $colors = array('green', 'red', 'blue'); ksort($colors); print_r($colors); Output : Array ( [0] => green [1] => red [2] => blue )

Multidimensional Arrays Arrays can contain other arrays. Example: $fruits = array('apples', 'bananas', 'oranges'); $meats = array('steaks', 'hamburgers', 'hotdogs'); $groceries = array ('Fruit'=>$fruits, 'Meat'=>$meats);

Multidimensional Arrays Example: Array ( [Fruit] => Array ( [0] => apples [1] => bananas [2] => oranges ) [Meat] => Array ( [0] => steaks [1] => hamburgers [2] => hotdogs ) );

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

References References : 1.Anonymous.(n.d.). Apache HTTP Server Documentation Version 2.2. Retrieved from Achour, M., Betz, F. (n.d.), PHP Manual. Retrieved from Anonymous. (n.d.). MySQL Reference Manual. Retrieved from 4.Naramore, E., Gerner, J., Le Scouarnec, Y., Stolz, J., Glass, M. K. (2005). Beginning PHP5, Apache, and MySQL® Web Development. Indianapolis, IN: Wiley Publishing, Inc.