Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.

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

SWU, Computer systems and technologies. The Objective of This Lecture To give you a very high-level overview of some of the tools for Web Programming.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
XAMPP: Cross – Apache, MySQL, Php, Perl + FileZilla, Tomcat NetBeans: IDE PHP Installation.
ITC 240: Web Application Programming
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
04/09/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
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.
Mandakini Ayushi Infotech Mandakini Kumari 22 nd July PHP Basic.
Decisions, Loops, and Arrays Achmad Arwan, S.Kom.
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.
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.
PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
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.
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.
Class 2Intro to Databases Goals of this class Include & Require in PHP Generating Random Numbers in PHP Arrays – Numerically Indexed and Associative Program.
Web Programming Language Week 5 Dr. Ken Cosh Introducing PHP 1.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Julian Springer Room 42 Joe Slovo.  W3 schools recommend that you have a basic understanding of HTML and of JavaScript before attempting to grasp PHP.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
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.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
PHP Arrays By Justin Nelsen. What is an Array? - An array can store one or more values in a single variable name. -Each element in the array is assigned.
PHP Constructs Advance Database Management Systems Lab no.3.
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.
Introduction to PHP.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Perl Day 5. Arrays vs Hash Arrays are one way to store multiple things in a variable. Hashes are another. Arrays are one way to store multiple things.
ITM ITM 352 Array Processing. ITM Simple Array Processing  Arrays can be processed using iteration. Standard array-processing loop:  What.
ITM © Port, Kazman1 ITM 352 Simple Arrays. ITM © Port, Kazman2 Arrays r Collections r Array basics m Declaration, allocation, and initialization.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
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. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
1 Server Side scripting PHP. 2 What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are.
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
Arrays and Strings. Arrays in PHP Arrays are made up of multiple memory blocks, each with the same name and differentiated by an index number Each block.
PHP using MySQL Database for Web Development (part II)
Arrays An array in PHP is an ordered map
PHP Arrays Functions.
CHAPTER 5 SERVER SIDE SCRIPTING
8th Semester, Batch 2008 Department of Computer Science SSUET.
While Loops BIS1523 – Lecture 12.
Basic PHP Lecture by Nutthapat Keawrattanapat
Basics (Cont...).
PHP an introduction.
PHP PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA),
PHP Array.
Presentation transcript:

Outline o What is an array ? o Indexed array o Associative array o Multidimensional array

What is an Array?  An array is a special variable, which can store multiple values in one single variable.  An array can hold many values under a single name, and you can access the values by referring to an index number.

What is an array?  However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?  The best solution here is to use an array!  An array can hold all your variable values under a single name. And you can access the values by referring to the array name.  Each element in the array has its own index so that it can be easily accessed.

What is an array?  In PHP, the array() function is used to create an array: array();

Kind of arrays in PHP Indexed array - An array with a numeric index Associative array - An array where each ID key is associated with a value Multidimensional array - An array containing one or more arrays

Indexed Arrays  A numeric array stores each array element with a numeric index.  There are two ways to create indexed arrays:

Indexed Arrays 1. In the following example the index are automatically assigned (the index starts at 0): - $cars = array ("Saab","Volvo","BMW","Toyota"); 1. The following example we assign the index manually:] 1. $cars[0]="Saab"; 2. $cars[1]="Volvo"; 3. $cars[2]="BMW"; 4. $cars[3]="Toyota"; $array_name = array(val1, val2, val3,val4,……);

Indexed Arrays - Example <?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0]. " and ". $cars[1]. " are Swedish cars."; ?>

Indexed Arrays - Example <?php $array = array(1, 2,3,4,5,6,7,8,9); for ($i = 0; $i < 9; $i++) { echo “Number : “. $array[$i]; } ?>

Print_r() Function  Print construction of array as group of locations and the value stored in it print_r ($expression [, bool $return = false ] )

Print_r() <?php $arr = array(1,2,3,4,5); Print_r($arr); ?> Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )

Count() Function  This is build in function,counts all elements in an array, or something in an object. int count ($var [, int $mode = COUNT_NORMAL ] )

Count Function - Example

Count Function - Example <?php $array = array(“M”, ”u”,”n”,”e”,”e”,”r”); for ($i = 0; $i < count($array); $i++) { echo $array[$i]; } ?>

Foreach  For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value. foreach ($array as $value) { code to be executed; }

Foreach - Example <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value. " "; } ?>

Foreach - Example

in_array() Function  The in_array() function searches an array for a specific value.  This function returns TRUE if the value is found in the array, or FALSE otherwise. in_array(search,array,type) ParameterDescription searchRequired. Specifies the what to search for arrayRequired. Specifies the array to search typeOptional. If this parameter is set, the in_array() function searches for the search-string and specific type in the array Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

in_array() Function - Example <?php $os = array("Mac", “W7", “W8", "Linux"); if (in_array(“XP", $os)) { echo "Got XP"; } if (in_array(“Mac", $os)) { echo "Got Mac"; } ?>

Associative Arrays  An associative array, each ID key is associated with a value.  When storing data about specific named values, a numerical array is not always the best way to do it.  With associative arrays we can use the values as keys and assign values to them.

Associative Arrays $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); or $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; $array_name= array($index1=>$val1, $index1=>$val1,…..);

Associative Arrays "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?>

Associative Arrays - example 43, 32, 56, "b" => 12); //...this array array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?>

Associative Arrays - example

Associative Arrays - example 1, 12 => 2); $arr[] = 56; // This is the same as $arr[13] = 56; // at this point of the script $arr["x"] = 42; // This adds a new element to // the array with key "x" unset($arr[5]); // This removes the element from the array unset($arr); // This deletes the whole array ?>

Associative Arrays - example array(6 => 5, 13 => 9, "a" => 42)); echo $arr["somearray"][6]; // 5 echo $arr["somearray"][13]; // 9 echo $arr["somearray"]["a"]; // 42 ?>

Multidimensional Arrays  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, $myArray = array( array( value1, value2, value3 ), array( value4, value5, value6 ), array( value7, value8, value9 ) );

Multidimensional Arrays array ( "a" => "orange", "b" => "banana“,"c" => "apple”),"numbers" => array ( 1, 2,3,4,5,6), "holes" => array ("first“,5 => "second“,"third ”)); echo $fruits["holes"][5]; // prints "second" echo $fruits["fruits"]["a"]; // prints "orange" ?>

Multidimensional Arrays <?php $families = array(“capital"=>array(“A“,“B“,“C”), “small"=>array(“a”, “b”, “c”), “number"=>array(“1“,“2“,”3”)); foreach($families as $value) {foreach($value as $values) {echo $values.” ”;} Echo “ ”} ?>

Multidimensional Arrays <?php $try[] = array("11", "12", "15", "22", "41", "42"); $try[] = array("6", "7", "16", "17", "22", "23"); $count = count ($try); for ($i=0; $i<$count; $i++) { $countmore=count($try[0]); for ($j=0; $j < $countmore; $j++) { print ("i$i j$j ". $try[$i][$j]. " "); } print (" "); } ?>

Multidimensional Arrays <?php $try[] = array("11", "12", "15", "22", "41", "42"); $try[] = array("6", "7", "16", "17", "22", "23"); $count = count ($try); for ($i=0; $i<$count; $i++) { $countmore=count($try[0]); for ($j=0; $j < $countmore; $j++) { print ("i$i j$j ". $try[$i][$j]. " "); } print (" "); } ?>