SlideShare a Scribd company logo
PHP Arrays
Dr. Charles Severance
www.wa4e.com
http://www.wa4e.com/code/arrays
http://www.wa4e.com/code/arrays.zip
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
http://en.wikipedia.org/wiki/Associative_array
Associative Arrays
• Can be key => value or simply indexed by numbers
• Ignore two-dimensional arrays for now...
http://en.wikipedia.org/wiki/Associative_array
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() shows PHP data - it is good for
debugging
<?php
$stuff = array("name" => "Chuck",
"course" => "PHPIntro");
echo("<pre>n");print_r($something);echo("n</pre>n");
?>
Array(
[name] => Chuck
[course] => PHPIntro
)
Building up an Array
• You can allocate a new item in the array and append 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
PHP-04-Arrays.ppt
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 setn" : "name is not setn";
echo isset($za['addr']) ? "addr is setn" : "addr is not setn";
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)
http://www.wa4e.com/code/arrays/get-01.php
PHP-04-Arrays.ppt
PHP-04-Arrays.ppt
PHP-04-Arrays.ppt
Web Server Database Server
Time
Apache
PHP
MySql
Browser
JavaScri
pt
D
O
M
php
code
static
files
RRC/HTTP SQL
Parse
Respons
e
Parse
Reques
t
ind.ph
p
$_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 email address strpos($var, '@') > 0
• ....
http://www.wa4e.com/code/arrays/guess.php?guess=7
http://www.wa4e.com/code/arrays/guess.php?guess=200
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 2010- Charles R. Severance
(www.dr-chuck.com) as part of www.wa4e.com 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

More Related Content

PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PPTX
PHP array 1
Mudasir Syed
 
PPT
PHP array 2
Mudasir Syed
 
PPT
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PPTX
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
PPTX
Arrays in PHP
davidahaskins
 
PDF
Array String - Web Programming
Amirul Azhar
 
Chapter 2 wbp.pptx
40NehaPagariya
 
PHP array 1
Mudasir Syed
 
PHP array 2
Mudasir Syed
 
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
Arrays in PHP
davidahaskins
 
Array String - Web Programming
Amirul Azhar
 

Similar to PHP-04-Arrays.ppt (20)

PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PDF
PHP tips and tricks
Damien Seguy
 
PPTX
Unit 2-Arrays.pptx
mythili213835
 
PDF
Php tips-and-tricks4128
PrinceGuru MS
 
PDF
PHP Basic & Arrays
M.Zalmai Rahmani
 
DOCX
Array andfunction
Girmachew Tilahun
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PDF
Php array
Nikul Shah
 
PPT
05php
anshkhurana01
 
PPT
PHP and MySQL with snapshots
richambra
 
PPTX
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
PPTX
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
PHP Arrays_Introduction
To Sum It Up
 
PPTX
Arrays in php
soumyaharitha
 
PPT
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
PPT
Php classes in mumbai
Vibrant Technologies & Computers
 
PPT
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
PPTX
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
4.1 PHP Arrays
Jalpesh Vasa
 
PHP tips and tricks
Damien Seguy
 
Unit 2-Arrays.pptx
mythili213835
 
Php tips-and-tricks4128
PrinceGuru MS
 
PHP Basic & Arrays
M.Zalmai Rahmani
 
Array andfunction
Girmachew Tilahun
 
Php array
Nikul Shah
 
PHP and MySQL with snapshots
richambra
 
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
Arrays in php
Laiby Thomas
 
PHP Arrays_Introduction
To Sum It Up
 
Arrays in php
soumyaharitha
 
Php my sql - functions - arrays - tutorial - programmerblog.net
Programmer Blog
 
Php classes in mumbai
Vibrant Technologies & Computers
 
PHP - Introduction to PHP
Vibrant Technologies & Computers
 
Introduction to PHP Lecture 1
Ajay Khatri
 
Arrays syntax and it's functions in php.pptx
NikhilVij6
 
Ad

Recently uploaded (20)

PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
PDF
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
PPTX
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Software Development Methodologies in 2025
KodekX
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Event Presentation Google Cloud Next Extended 2025
minhtrietgect
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Building High-Performance Oracle Teams: Strategic Staffing for Database Manag...
SMACT Works
 
How to Build a Scalable Micro-Investing Platform in 2025 - A Founder’s Guide ...
Third Rock Techkno
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Best ERP System for Manufacturing in India | Elite Mindz
Elite Mindz
 
Ad

PHP-04-Arrays.ppt

  • 1. PHP Arrays Dr. Charles Severance www.wa4e.com http://www.wa4e.com/code/arrays http://www.wa4e.com/code/arrays.zip
  • 2. 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 http://en.wikipedia.org/wiki/Associative_array
  • 3. Associative Arrays • Can be key => value or simply indexed by numbers • Ignore two-dimensional arrays for now... http://en.wikipedia.org/wiki/Associative_array
  • 4. Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "n"; ?> There
  • 5. Key / Value <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo $stuff["course"], "n"; ?> PHPIntro
  • 6. Dumping an Array • The function print_r() shows PHP data - it is good for debugging <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo("<pre>n");print_r($something);echo("n</pre>n"); ?> Array( [name] => Chuck [course] => PHPIntro )
  • 7. Building up an Array • You can allocate a new item in the array and append 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 )
  • 8. 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 )
  • 9. 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
  • 10. 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
  • 13. 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
  • 14. $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 setn" : "name is not setn"; echo isset($za['addr']) ? "addr is setn" : "addr is not setn"; Count: 2 $za Is an array name is set addr is not set
  • 15. $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 )
  • 16. $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 )
  • 17. 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 )
  • 19. 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)
  • 24. Web Server Database Server Time Apache PHP MySql Browser JavaScri pt D O M php code static files RRC/HTTP SQL Parse Respons e Parse Reques t ind.ph p $_GET
  • 25. 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 email address strpos($var, '@') > 0 • ....
  • 28. 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
  • 29. Acknowledgements / Contributions These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com 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

Editor's Notes

  • #2: Note from Chuck. If you are using these materials, you can remove my name and URL from this replace it with your own, but please retain the CC-BY logo on the first page as well as retain the entire last page when you remix and republish these slides.
  • #30: Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.