Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.

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

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
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.
Arrays Strings and regular expressions Basic PHP Syntax CS380 1.
Pemrograman Web Dasar Pertemuan 12 PHP Array. Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional.
PHP Using Arrays.
ITC 240: Web Application Programming
PHP Functions Composite Types. Functions  Declaration  function functionName(paramList) {  /* code goes here */ }  paramList is comma-separated list.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2010 All Rights Reserved. 1.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
PHP & MySQL Mahak Arora Vivek Bangera. Outline How PHP works Basic scripting in PHP Forms in PHP(GET & POST Variables) SQL basics PHP and MySQL connection.
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:
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 H ypertext P re-processor. Unit 6 - PHP - Hello World! - Data types - Control structures - Operators.
Arrays IDIA Spring 2012 Bridget M. Blodgett.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
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.
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.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
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:
Syntax : If ( Expression ) If ( Expression ) {Statements; } Example : ?>
PHP Arrays Dr. Charles Severance
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.
PHP - 1h. How it works Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML.
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
Martin Kruliš by Martin Kruliš (v1.1)1.
CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
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.
PHP Arrays Dr. Charles Severance
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Introduction to PHP 1.What is PHP? What Is PHP?  php: hypertext preprocessor  Server-side scripting language—like ASP—scripts are executed on server.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
CSE 143 Lecture 11: Sets and Maps reading:
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.
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
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.
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.
CGS 3066: Web Programming and Design Spring 2016 PHP.
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 using MySQL Database for Web Development (part II)
Oleh: Ahmad Ramadhani, S.Kom
Web Design 2 Using PHP PHP Arrays.
PHP Arrays Dr. Charles Severance
PHP Arrays Functions.
CHAPTER 5 SERVER SIDE SCRIPTING
Class06 Arrays MIS 3502 Jeremy Shafer Department of MIS
PHP Arrays Dr. Charles Severance
Web System & Technology
Arrays MIS 3502 Jeremy Shafer Department of MIS Fox School of Business
Perl Variables: Hash Web Programming.
Web Systems Development (CSC-215)
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
PHP Array.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Unit – II Part II Scripting Essentials PHP Functions Arrays
Presentation transcript:

Array & Foreach อาร์เรย์และคำสั่งวนลูป

Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions

Definition and Usage The array() function is used to create an array. In PHP, there are three types of arrays:  Indexed arrays - Arrays with numeric index  Associative arrays - Arrays with named keys  Multidimensional arrays - Arrays containing one or more arrays

Syntax Syntax for indexed arrays: Syntax for associative arrays: array(value1,value2,value3,etc.); array(key=>value,key=>value,key=>value,etc.);

Syntax

<?php $cars=array("Volvo","BMW","Toyota"); echo "I like ". $cars[0]. ", ". $cars[1]. " and ". $cars[2]. "."; // การใช้เครื่องหมาย. เป็นการเชื่อมประโยคเข้าด้วยกัน ?> Syntax

"35","Ben"=>"37","Joe"=>" 43"); echo "Peter is ". $age['Peter']. " years old."; ?> Syntax

"35","Ben"=>"37","Joe"=>" 43"); echo "Peter is ". $age['Peter']. " years old."; ?> Syntax

PHP 4, PHP 5 print_r — Prints human-readable information about a variable print_r Statement Source:

<?php $b = array ('m' => 'monkey', 'foo' => 'bar', 'x' => array ('x', 'y', 'z')); $results = print_r($b); // $results now contains output from print_r ?> print_r Statement Array ( [m] => monkey [foo] => bar [x] => Array ( [0] => x [1] => y [2] => z ) )

"; } ?> For() Loop through and print all the values of an indexed array:

"35","Ben"=>"37","Joe"=>" 43"); foreach($age as $x=>$x_value) { echo "Key=". $x. ", Value=". $x_value; echo " "; } ?> Foreach() Loop through and print all the values of an associative array:

Array Functions

count($ar) - How many elements in an array is_array($ar) - Returns TRUE if a variable is an array sort($ar) - Sorts the array values (loses key) ksort($ar) - Sorts the array by key asort($ar) - Sorts array by value, keeping key association shuffle($ar) - Shuffles the array into random order

Array and String $txt = “This is a book !”; $ar = explode(' ', $txt); print_r($ar); Array ( [0] => This [1] => is [2] => a [3] => book! )

Summary PHP arrays are a very powerful associative array as they can be indexed by integers like a list, or use keys to look values up like a hash map or dictionary There are many options for sorting We can use explode() to split a string into an array of strings

Q & A