PHP Arrays Dr. Charles Severance

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 PHP Dr. Charles Severance
PHP Functions / Modularity
JavaScript Functions, Objects, Array, and Control Structures Dr. Charles Severance
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Expressions and Control Flow in PHP Dr. Charles Severance
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Forms and PHP Dr. Charles Severance
Cookies, Sessions, and Authentication Dr. Charles Severance
UFCEKG-20-2 Data, Schemas & Applications Lecture 4 Server Side Scripting & PHP.
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.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
HTML, PHP, and MySQL: Putting It All Together. Making a Form Input tags Types: “text” “radio” “checkboxes” “submit”
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.
Variables, Expressions, and Statements
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Introduction to Dynamic Web Content Dr. Charles Severance
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
PHP Functions Dr. Charles Severance
JavaScript Dr. Charles Severance
Expressions and Control Flow in PHP. Expressions Expressions evaluate to a value. The value can be a string, number, boolean, etc... Expressions often.
Forms and PHP Dr. Charles Severance
Relational Databases and MySQL Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to JavaScript MIS 3502, Spring 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 2/2/2016.
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
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,
CGS 3066: Web Programming and Design Spring 2016 PHP.
Introduction to Dynamic Web Content Dr. Charles Severance
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
Introduction to Dynamic Web Content Chapter 1 Dr. Charles Severance To be used in assocition with the book: PHP, MySql, and JavaScript by Robin Nixon.
Introduction to JavaScript MIS 3502, Fall 2016 Jeremy Shafer Department of MIS Fox School of Business Temple University 9/29/2016.
Dr. Charles Severance Using Handlebars Dr. Charles Severance
Web Design 2 Using PHP PHP Arrays.
Introduction to Dynamic Web Content
PHP Arrays Dr. Charles Severance
Expressions and Control Flow in PHP
Our Technologies Dr. Charles Severance
Reading Files Chapter 7 Python for Everybody
PHP Functions / Modularity
Getting PHP Hosting Dr. Charles Severance
Using JSON Dr. Charles Severance
Introduction to PHP Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Everybody
Introduction to PHP Dr. Charles Severance
HTTP Parameters and Arrays
Python Lists Chapter 8 Python for Everybody
Functions Chapter 4 Python for Everybody
PHP Arrays Dr. Charles Severance
Object Oriented Programming in JavaScript
Expressions and Control Flow in PHP
Tuples Chapter 10 Python for Everybody
PHP Namespaces (in progress)
Introduction to Dynamic Web Content
Using jQuery Dr. Charles Severance
FMI-PLOVDIV Web Dynamic Applications
Charles Severance Single Table SQL.
PHP an introduction.
Data Modelling Many to Many
Model View Controller (MVC)
Presentation transcript:

PHP Arrays Dr. Charles Severance

PHP Arrays Rock! Better than Python Dictionaries Better than Java Hash Maps PHP Arrays have all the benefits of Python Dictionaries but they can also maintain the order of the items in the array

Associative Arrays Like Python Dictonaries - but more powerful Can be key => value or simply indexed by numbers Ignore two-dimensional arrays for now...

Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "\n"; ?> There

Key / Value <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo $stuff["course"], "\n"; ?> PHPIntro

Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo(" \n");print_r($something);echo("\n \n"); ?> Array( [name] => Chuck [course] => PHPIntro )

Building up an Array You can allocate a new item in the array and add a value at the same time using empty square braces [] on the right hand side of an assignment statement $va = array(); $va[] = "Hello"; $va[] = "World"; print_r($va); Array( [0] => Hello [1] => World )

Building up an Array You can also add new items in an array using a key as well $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; print_r($za); Array( [name] => Chuck [course] => PHPIntro )

Looping Through an Array <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=name Val=Chuck Key=course Val=PHPIntro

Arrays of Arrays $products = array( 'paper' =>array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'ball'=> "Ball Point", 'hilite' => "Highlighters", 'marker' => "Markers"), 'misc' => array( 'tape'=> "Sticky Tape", 'glue'=> "Adhesives", 'clips' => "Paperclips") ); The elements of an array can be many things other than a string or integer. You can even have objects or other arrays. echo $products["pens"]["marker"]; Markers

Array Functions

count($ar) - How many elements in an array is_array($ar) - Returns TRUE if a variable is an array isset($ar['key']) - Returns TRUE if key is set in the 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

$za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; print "Count: ". count($za). "\n"; if ( is_array($za) ) { echo '$za Is an array'. "\n"; } else { echo '$za Is not an array'. "\n"; } echo isset($za['name']) ? "name is set\n" : "name is not set\n"; echo isset($za['addr']) ? "addr is set\n" : "addr is not set\n"; Count: 2 $za Is an array name is set addr is not set

$za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; $za["topic"] = "PHP"; print_r($za); sort($za); print_r($za); Array( [name] => Chuck [course] => PHPIntro [topic] => PHP ) Array( [0] => Chuck [1] => PHP [2] => PHPIntro )

$za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; $za["topic"] = "PHP"; print_r($za); ksort($za); print_r($za); asort($za); print_r($za); Array( [name] => Chuck [course] => PHPIntro [topic] => PHP ) Array( [course] => PHPIntro [name] => Chuck [topic] => PHP ) Array( [name] => Chuck [topic] => PHP [course] => PHPIntro )

Arrays and Strings $inp = "This is a sentence with seven words";$temp = explode(' ', $inp); print_r($temp); Array( [0] => This [1] => is [2] => a [3] => sentence [4] => with [5] => seven [6] => words )

HTTP Parameters and Arrays

PHP Global Variables Part of the goal of PHP is to make interacting with HTTP and HTML as easy as possible PHP processes the incoming HTTP Request based on the protocol specifications and drops the data into various super global variables (usually arrays)

Web ServerDatabase Server Time Apache PHP MySql Browser JavaScri pt DOMDOM ind.php Parse Respons e Parse Reques t $_GET

Validation Making sure all user data is present and the correct format before proceeding Non empty strlen($var) > 0 A number is_numeric($var) An address strpos($var, > 0....

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 PHP arrays maintain order and there are many options for sorting We can use explode() to split a string into an array of strings HTTP Information is pre-processed and placed in super global arrays

Acknowledgements / Contributions These slides are Copyright Charles R. Severance ( as part of and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here