SlideShare a Scribd company logo
1 of 13
PHP Arrays
Web Technology
Made By:
Tarang Desai
140950107100
CSE – B
6th
Semester
Introduction to Array
• Arrays is a collection of similar type of elements, but in PHP you can
have the elements of mixed type together in single array.
• In each PHP, each element has two parts:
• Key
• Value
• The key represents the index at which the value of element can be
stored.
• The keys are positive integers that are in ascending order.
Array Creation
• There are many ways to create an array in PHP.
• The first way is to use the current array.
• $mylist=array(10,20,30,40,50);
• The second way is to assign the value directly to the array.
• $mylist[0]=10;
• An empty array can also be created using the array construct.
• $mylist=array();
• We can have mixed type of elements in the array.
• $mylist=array(“Tarang”=>”Vadodara”,”Mandar”=>7.5);
Accessing Array Elements
• Using an array subscript we can access the array element. The value
of subscript is enclosed within the square brackets.
• $Citycode[‘Vadodara’]=0265;
• $Name[0]=“Tarang”;
• Multiple values can be set to a single scalar variable using array.
• $people=array(“Tarang”,”Mandar”,”Shivang”);
• List($operator,$accountant,$manager)=$people;
• By this assignment Tarang becomes operator, Mandar becomes
accountant and Shivang becomes the manager.
Types of Array in PHP
PHP supports three types of array:
•Numeric Arrays
•Associative Arrays
•Multidimensional Arrays
Numeric Array
• A numeric array stores each element with a numeric ID key.
• There are two ways to write a numeric array.
• Automatically
• $names = array("Peter","Quagmire","Joe");
• Manually
• $names[0] = "Peter"; $names[1] = "Quagmire";
• $names[2] = "Joe";
• $name[] =”Peter”; $name[] =“Quagmire”; $name[] = “joe”;
Example
Code
Output
Associative Array
• 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.
• Example: Using array to assign age to a person.
• $ages = array(”Brent"=>42, ”Andrew"=>25, "Joshua”=>16);
• $ages[ Brent'] = 42;‟
• $ages['Joshua'] = 16;
Example
Code
Output
Multidimensional Array
• 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.
• Example:
• $shopping=array(array(“Banana”,30),array(Orange,70),array(“Mango”,100));
• echo $shopping[0][0].” costs Rs. “.$shopping[0][1].”<br/>”;
• echo $shopping[1][0].” costs Rs. “.$shopping[1][1].”<br/>”;
• echo $shopping[2][0].” costs Rs. “.$shopping[2][1].”<br/>”;
Example
Code
Output
Finding the Size of an Array
• The count() function is used to find the number of elements in the
array.
• Example:
• $icecream_menu=array('Almond Punch'=>275,'Nutty Crunch'=>160,'Choco
Dip'=>290,'Oreo Cherry'=>250);
• print "There are ".count($icecream_menu) ." flavours of icecreams
available!!"
• Output
• There are 4 flavours of icecreams available!!
Web Technology - PHP Arrays

More Related Content

What's hot (20)

JavaScript - Chapter 10 - Strings and Arrays
 JavaScript - Chapter 10 - Strings and Arrays JavaScript - Chapter 10 - Strings and Arrays
JavaScript - Chapter 10 - Strings and Arrays
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
File Management in Operating System
File Management in Operating SystemFile Management in Operating System
File Management in Operating System
 
Dhtml ppt (2)
Dhtml ppt (2)Dhtml ppt (2)
Dhtml ppt (2)
 
HTTP Request and Response Structure
HTTP Request and Response StructureHTTP Request and Response Structure
HTTP Request and Response Structure
 
Php array
Php arrayPhp array
Php array
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Web forms in ASP.net
Web forms in ASP.netWeb forms in ASP.net
Web forms in ASP.net
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
HTML
HTMLHTML
HTML
 
Java script errors &amp; exceptions handling
Java script  errors &amp; exceptions handlingJava script  errors &amp; exceptions handling
Java script errors &amp; exceptions handling
 
Uniform Resource Locator (URL)
Uniform Resource Locator (URL)Uniform Resource Locator (URL)
Uniform Resource Locator (URL)
 
Xml namespace
Xml namespaceXml namespace
Xml namespace
 
HTML Forms
HTML FormsHTML Forms
HTML Forms
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
1. web technology basics
1. web technology basics1. web technology basics
1. web technology basics
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 

Viewers also liked

History of Web Technology
History of Web TechnologyHistory of Web Technology
History of Web TechnologyShuvo Malakar
 
Introduction To Web Technology
Introduction To Web TechnologyIntroduction To Web Technology
Introduction To Web TechnologyArun Kumar
 
Seminar presentation on embedded web technology
Seminar presentation on embedded web technologySeminar presentation on embedded web technology
Seminar presentation on embedded web technologyRanol R C
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptAndres Baravalle
 
Web technologies lesson 1
Web technologies   lesson 1Web technologies   lesson 1
Web technologies lesson 1nhepner
 
Presentation on component based software engineering(cbse)
Presentation on component based software engineering(cbse)Presentation on component based software engineering(cbse)
Presentation on component based software engineering(cbse)Chandan Thakur
 
Introduction to Web Technology
Introduction to Web TechnologyIntroduction to Web Technology
Introduction to Web TechnologyAashish Jain
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScriptBryan Basham
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to JavascriptAmit Tyagi
 
multimedia technologies Introduction
multimedia technologies Introductionmultimedia technologies Introduction
multimedia technologies IntroductionMohammed Fareed
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An IntroductionManvendra Singh
 
telecommunication-ppt
telecommunication-ppttelecommunication-ppt
telecommunication-pptsecomps
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheLeslie Samuel
 

Viewers also liked (19)

History of Web Technology
History of Web TechnologyHistory of Web Technology
History of Web Technology
 
Web technology
Web technologyWeb technology
Web technology
 
Introduction To Web Technology
Introduction To Web TechnologyIntroduction To Web Technology
Introduction To Web Technology
 
Seminar presentation on embedded web technology
Seminar presentation on embedded web technologySeminar presentation on embedded web technology
Seminar presentation on embedded web technology
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Web technologies lesson 1
Web technologies   lesson 1Web technologies   lesson 1
Web technologies lesson 1
 
Presentation on component based software engineering(cbse)
Presentation on component based software engineering(cbse)Presentation on component based software engineering(cbse)
Presentation on component based software engineering(cbse)
 
Introduction to Web Technology
Introduction to Web TechnologyIntroduction to Web Technology
Introduction to Web Technology
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Js ppt
Js pptJs ppt
Js ppt
 
multimedia technologies Introduction
multimedia technologies Introductionmultimedia technologies Introduction
multimedia technologies Introduction
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
telecommunication-ppt
telecommunication-ppttelecommunication-ppt
telecommunication-ppt
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Multimedia
MultimediaMultimedia
Multimedia
 
How to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your NicheHow to Become a Thought Leader in Your Niche
How to Become a Thought Leader in Your Niche
 

Similar to Web Technology - PHP Arrays

Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04Hassen Poreya
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvZahouAmel1
 
Array,Array Function,Get Method,Post Method
Array,Array Function,Get Method,Post MethodArray,Array Function,Get Method,Post Method
Array,Array Function,Get Method,Post MethodJignesh Patel
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Mohd Harris Ahmad Jaal
 
JAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfJAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfcherop41618145
 
Processing & Dataviz
Processing & DatavizProcessing & Dataviz
Processing & Datavizblprnt
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structuresagorolabs
 

Similar to Web Technology - PHP Arrays (20)

PHP array 1
PHP array 1PHP array 1
PHP array 1
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
4.1 PHP Arrays
4.1 PHP Arrays4.1 PHP Arrays
4.1 PHP Arrays
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
 
Array,Array Function,Get Method,Post Method
Array,Array Function,Get Method,Post MethodArray,Array Function,Get Method,Post Method
Array,Array Function,Get Method,Post Method
 
Scripting3
Scripting3Scripting3
Scripting3
 
Array in php
Array in phpArray in php
Array in php
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
 
JAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfJAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdf
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
Processing & Dataviz
Processing & DatavizProcessing & Dataviz
Processing & Dataviz
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 

Recently uploaded

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

Web Technology - PHP Arrays

  • 1. PHP Arrays Web Technology Made By: Tarang Desai 140950107100 CSE – B 6th Semester
  • 2. Introduction to Array • Arrays is a collection of similar type of elements, but in PHP you can have the elements of mixed type together in single array. • In each PHP, each element has two parts: • Key • Value • The key represents the index at which the value of element can be stored. • The keys are positive integers that are in ascending order.
  • 3. Array Creation • There are many ways to create an array in PHP. • The first way is to use the current array. • $mylist=array(10,20,30,40,50); • The second way is to assign the value directly to the array. • $mylist[0]=10; • An empty array can also be created using the array construct. • $mylist=array(); • We can have mixed type of elements in the array. • $mylist=array(“Tarang”=>”Vadodara”,”Mandar”=>7.5);
  • 4. Accessing Array Elements • Using an array subscript we can access the array element. The value of subscript is enclosed within the square brackets. • $Citycode[‘Vadodara’]=0265; • $Name[0]=“Tarang”; • Multiple values can be set to a single scalar variable using array. • $people=array(“Tarang”,”Mandar”,”Shivang”); • List($operator,$accountant,$manager)=$people; • By this assignment Tarang becomes operator, Mandar becomes accountant and Shivang becomes the manager.
  • 5. Types of Array in PHP PHP supports three types of array: •Numeric Arrays •Associative Arrays •Multidimensional Arrays
  • 6. Numeric Array • A numeric array stores each element with a numeric ID key. • There are two ways to write a numeric array. • Automatically • $names = array("Peter","Quagmire","Joe"); • Manually • $names[0] = "Peter"; $names[1] = "Quagmire"; • $names[2] = "Joe"; • $name[] =”Peter”; $name[] =“Quagmire”; $name[] = “joe”;
  • 8. Associative Array • 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. • Example: Using array to assign age to a person. • $ages = array(”Brent"=>42, ”Andrew"=>25, "Joshua”=>16); • $ages[ Brent'] = 42;‟ • $ages['Joshua'] = 16;
  • 10. Multidimensional Array • 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. • Example: • $shopping=array(array(“Banana”,30),array(Orange,70),array(“Mango”,100)); • echo $shopping[0][0].” costs Rs. “.$shopping[0][1].”<br/>”; • echo $shopping[1][0].” costs Rs. “.$shopping[1][1].”<br/>”; • echo $shopping[2][0].” costs Rs. “.$shopping[2][1].”<br/>”;
  • 12. Finding the Size of an Array • The count() function is used to find the number of elements in the array. • Example: • $icecream_menu=array('Almond Punch'=>275,'Nutty Crunch'=>160,'Choco Dip'=>290,'Oreo Cherry'=>250); • print "There are ".count($icecream_menu) ." flavours of icecreams available!!" • Output • There are 4 flavours of icecreams available!!