PHP Arrays. Outline o What is array in PHP ? o Numeric 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.
Functions & Arrays. Functions Functions offer the ability for programmers to group together program code that performs specific task or function into.
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
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.
Outline o What is an array ? o Indexed array o Associative array o Multidimensional array.
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.
PHP - Hypertext Preprocessor. Introduction PHP is a powerful server-side scripting language for creating dynamic and interactive websites. PHP is a powerful.
Arrays IDIA Spring 2012 Bridget M. Blodgett.
Nael Alian Introduction to PHP
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.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
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.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 5 Server Side Scripting Perl.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
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.
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.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
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.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Introduction to PHP.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
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.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
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.
CGS 3066: Web Programming and Design Spring 2016 PHP.
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.
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
PHP Arrays By Justin Nelsen.
8th Semester, Batch 2008 Department of Computer Science SSUET.
PHP Introduction.
While Loops BIS1523 – Lecture 12.
Basic PHP Lecture by Nutthapat Keawrattanapat
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Basics (Cont...).
PHP an introduction.
PHP PROF. S. LAKSHMANAN, DEPT. OF B. VOC. (SD & SA),
PHP Array.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

PHP Arrays

Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array

What is array in PHP ?  A variable is a storage area holding a number or text. The problem is, a variable will hold only one value.  An array is a special variable, which can store multiple values in one single variable.

What is array in PHP ?  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.

Kind of arrays in PHP Numeric 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

Numeric Arrays A numeric array stores each array element with a numeric index. There are two methods to create a numeric array:

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

Numeric Arrays-example

Numeric 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() Print construction of array as group of locations and the value stored in it Print_r($array );

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() this is build in function, that count number of value store in array or number of character in string. Count($variable );

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

foreach <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value. " "; } ?> foreach ($array as $value) { code to be executed; }

Numeric Arrays-example

Find in Array - This function return true if the $search found in $orginal_data, otherwise return false. in_array($search, $orginal_data)

Find in Array <?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“,"thir d”)); 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 (" "); } ?>