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.

Slides:



Advertisements
Similar presentations
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Understanding the Need for Sorting Records
Data Structure Dr. Mohamed Khafagy.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
XAMPP: Cross – Apache, MySQL, Php, Perl + FileZilla, Tomcat NetBeans: IDE PHP Installation.
NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Programming Logic and Design Fourth Edition, Comprehensive
2.3 Cool features in C# academy.zariba.com 1. Lecture Content 1.Extension Methods 2.Anonymous Types 3.Delegates 4.Action and Func 5.Events 6.Lambda Expressions.
Important Problem Types and Fundamental Data Structures
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
PhP Tutorial (3). Working with Arrays – Strings in PHP What is an Array.
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:
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
PHP Arrays. Outline o What is array in PHP ? o Numeric array o Associative array o Multidimensional array.
Lists in Python.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Lecture 5 – Function and Array SFDV3011 – Advanced Web Development 1.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
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.
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.
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.
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.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, © Copyright 2015, Fred McClurg, All Rights.
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.
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.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Unit 12 JavaScript Arrays Instructor: Brent Presley.
Computer Programming for Engineers
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming javascript arrays.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
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.
CSE 154 LECTURE 15: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
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.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
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 (Extra slides) Arrays are objects that help us organize large amounts of information.
Test 2 Review Outline.
Arrays.
PHP Arrays By Justin Nelsen.
8th Semester, Batch 2008 Department of Computer Science SSUET.
Arrays … The Sequel Applications and Extensions
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
STL - Algorithms.
Basics (Cont...).
Arrays.
Presentation transcript:

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 ID so that it can be easily accessed. $array[key] = value; There are 3 kinds of arrays 1.Numeric Array. 2. Associative Array. 3. Multidimensional Array.

Numeric Array Example: <?php $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; echo $names[1]. " and ". $names[2]. " are ". $names[0]. "'s neighbors"; ?> A numeric array stores each element with a numeric ID key. $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe";

Associative Array <?php $ages[ ‘ Brent ’ ] = ” 42"; $ages[ ‘ Andrew ’ ] = ” 25"; $ages[ ‘ Joshua ’ ] = ” 16"; echo Brent is ". $ages[ ‘ Brent ’ ]. " years old."; ?> 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.

Multidimensional Array $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); 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.

Defining Arrays Array in PHP may be defined by two types of patterns, defining and assigning one element at a time. The other one is declaring, which includes a definition and initial assignment of one to many elements. Print_r(): This function does two things it prints the content of PHP arrays as name and value pairs; Boolean print_r(mixed var [, Boolean mode])

Defining Arrays <?php $a = “dr.”; //declaring string variables $b = “mr.”; $c = “mrs.”; $salutation = array(); // define an array with zero numbers. $salutation[0] = $a; // Assigning elements using index $salutation[0] = $b; $salutation[0] = $c; Print_r($salutation); ?>

Managing Arrays PHP arrays have a number of predefined functions that enable you to manage arrays. There are four types of functions 1. Identification functions, such as count() and is_array() 2. Seeding and padding arrays with range() and array_pad() functions. 3. Queue Management funcitons. 4. Traversing functions

Identification Functions Syntax of count() function as follows. Int count(mixed variable [, int mode]) The count function enables you to count the number of elements. Syntax of is_array() function as follows Boolean is_array(mixed var) The is_array() function to get a count of all elements in a multidimensional array.

Seeding and Padding Arrays The range() function enables you to create and fill an array of numbers or characters based on a beginning value and an ending value. The syntax of range function as follows array range(mixed low, mixed high [, int step]) The range() function returns an array, which may be numerically indexed with a list of numbers or characters.

Queue Management Functions Arrays are sometimes managed as first-in-first-out (FIFO), first-in-last-out (FILO), last-in-first-out(LIFO), and last-in-last-out(LILO) queues there are four predefined commands to support queues, those are discussed below. Array_pop(): The function removes and returns the last element from a target array, which shorten the array by 1. Syntax for the above function as follows mixed array_pop(array var);

Array_push(): The function adds one or more elements at the end of a target array and returns 1 for success. Syntax for the function as follows int array_push(array var, mixed var[, mixed…..]) Array_shift(): The function removes and returns the first element from a target array, which shorten the array by 1. The syntax for the function as follows mixed array_shift(array var)

Array_unshift(): The function adds an element to the front of a target array and return 1 for success. The syntax for the function is as follows Int array_unshift ( array var, mixed var[, mixed …])

Traversing Functions There are seven predefined traversing functions all of them are explained below Current(): The function takes one formal parameter, which is a target array. The current() function returns the value of the current element. Syntax for the current function is as follows mixed current(array var)

Traversing Functions Each(): The function takes one formal parameter, which is a target array, and returns an array of four values. The syntax for the each() function is as follows array each(array var) End(): The function moves the pointer to the last element of the array and returns the value of the last element. The syntax is as follows mixed end(array var)

Traversing Functions Key(): The function takes one formal parameter, which is a target array. The key() functions returns the key of current element. Syntax for the function is below mixed key(array var) Next(): The function takes one formal parameter, which is a target array, and returns the next element in the array by moving the pointer forward. mixed next(array var)

Traversing Functions Prev(): The function takes one formal parameter, which is a target array, and return the previous element in the array by moving the pointer backward. Syntax for the above function is as follows mixed prev(array var) Reset(): The function moves the pointer to the first element of the array and returns the value of the first element. Syntax is as follows mixed reset(array var)

Sorting Arrays Sorting is very important function in arrays, there are some functions which supports sorting in PHP programming, those are explained below. Arsort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in reverse or descending order, preserving the native keys of the original array, and has this syntax: void arsort(array var [, int sort_flag])

Sorting Arrays Asort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in ascending order, preserving the native keys of the original array, and has this syntax void asort(array var [, int sort_flag]) Array_multisort(): The function takes a list of formal parameters that can include an array, a sort order flag, and sort type flag, followed by other array values. There can only be one actual parameter for the order or type flag value. Subsequent values will not raise error but will not be recognized or processed in PHP 5.0 and 5.1. Syntax: Boolean array_multisort(array var1 [, mixed order[, mixed type [, array var2]]..])

Sorting Arrays Natcasesort(): The function takes one formal parameter, which is an array. Itsorts in ascending order by the values and uses rules like those in the natsort() function. The function does preserve keys during sorting and differs from the natsort() in ignoring case during sort operations. It has the following Syntax: void natcasesort(array var).

Sorting Arrays Rsort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in descending or reverse order and reassigns native keys to new sequenced numeric key lists. It has this syntax void rsort(array var [, int sort_flag]). Sort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in ascending order but reassigns the native keys to new sequenced numeric key lists. It has this Syntax void sort(array var [, int sort_flag])

Merging and Partitioning Arrays Merging arrays joins two or more arrays into a single array. Partitioning carves data from arrays, there are some functions which are used to implement the merging concepts. Array_chunk(), array_combine(),array_diff(),array_diff_key(), array_diff_uassoc(), array_diff_ukey(), array_filter(), array_flip(), array_intersect(), array_intersect_assoc(), array_intersect_key(), array_intersect_ukey(), array_merge(), array_splice(). Every function is having importance.