SlideShare una empresa de Scribd logo
1 de 20
Descargar para leer sin conexión
PHP Arrays
By:- Jalpesh Vasa
What is Array?
• Arrays are lists: list of people;
list of size;
list of books;
• To store a group of related items in a single
variable is called....
• Types of array:
 Numeric array(or just an array)
 Associative array
 Multidimensional array
 Mixed array
What is Array? (contd.)
Numeric array(or just an array)
if you want to find and entry, you need to know
its position(index) within an array.
Eg. a[0], a[7], a[34]
Associative array(also known as Hash)
in this index are not integer, but strings.
Eg. a*“honest”+ = (“Mahatma Gandhi”);
a*“good_man”+ = (“Abraham lincoln”);
How to create Array?
You can create PHP array by using array()
function.
$num = array(5,4,3,2,1);
$word = array(“web”, “db”, “app”);
echo $num[2] = ?
echo $word[1] = ?
How to create Array? (contd.)
Also array is produced by:
$num[0] = 5;
$num[1] = 4;
$num[2] = 3;
Another way is:
$shopping = array();
$shopping*+ = “milk”;
$shopping*+ = “coffee”;
$shopping*+ = “potato”;
How to create Array? (contd.)
When you want to print an array in a double-quoted
string, you need to use the braces.
echo “the first item in the list is ,$shopping*0+-”;
print out entire contents of an array using
print_r(arr_name) function.
print_r($shopping); // used only for debugging purpose
How to create Array? (contd.)
PHP array is an ordered set of variable in which
each variable – called an element – has an
associated key.
PHP automatically assigns a key values if keys are
not specified when arrays are constructed.
$f*+ = “v1”
$f*+ = “v2”
$f*5+ = “v3”
$f*+ = “v4”
Keys
0
1
5
6
How to create Array? (contd.)
In associative array, you can access elements in
an array by name.
$abc = array(
name=>”jil”,
occupation=>”Business”,
age=>30
);
echo $abc[age];
How to create Array? (contd.)
Also array is produced by:
$abc*name+=“jil”;
$abc*occupation+=“business”;
$abc[age] = 30;
echo $abc[age];
How to create Array? (contd.)
Multidimensional array is....array within a
array.
Eg.
$abc = array(
array(name=>”bob”, age=>25),
array(name=>”alice”, age=>35),
array(name=>”greg”, age=>45)
);
print $abc[0][age];
How to create Array? (contd.)
$f*‘a’+ = 1;
$f*‘b’+ = 2;
$f*‘c’+ = 3;
$f*‘d’+ = 4;
$f*‘b’+ = $f*‘a’+ +$f*‘c’+ // ans is 4
$var = “the value of $f*‘b’+ = ,$f*‘b’+-”;
How to create Array? (contd.)
$a1 = array(‘a’, ‘b’, ‘c’);
= array(‘a’=>1, ‘a’, ‘b’=>2, ‘b’, ‘c’=>3, ‘c’);
= array(1=>“Sunday”, “Monday”, “Tuesday”);
=array(‘key’=>‘value’, ‘key2’=>array(1,2,3));
How to Print Array?
To print simple array:
For($i=0; $i<count($a1);$i++)
{
echo “the value at index $i = {$a1[$i+-”;
}
How to Print Array?
<?php
$my = array(“php”, “is”, “cool”);
foreach($my as $k => $val)
{
echo “the value of index $k is:$val”;
}
foreach($my as $val)
{
echo “value:$val<br/>”;
}
Array Functions
Array_merge() – joining two arrays.
Eg.
$f1 = array(“a”, “b”, “c”);
$f2 = array(1,2,3);
$f3 = array_merge($f1,$f2);
foreach($f3 as $val)
{
echo “$val”;
}
Array Functions
Array_push() – add multiple variables to array.
- accepts an array & no. of further para.
- returns total number of elements in the array.
Eg.
$f1 = array(“a”, “b”, “c”);
$total = array_push($f1,1,2,3);
echo “there are $total elements in $f1”;
foreach($f1 as $val)
{
echo “$val”;
}
Array Functions
Array_slice() – allows you to extract chunk of an array.
- array, start position, length.
- doesn’t alter the array you passed to it
- returns new array containing elements
Eg.
$f1 = array(“a”, “b”, “c”, “d”, “e”, “f”);
$f2 = array_slice($f1,2,3);
foreach($f2 as $val)
{
echo “$val”;
}
Array Functions
sort() – sort numerically indexed array based on value
Rsort() – reverse sort
Don’t pass associative array
Eg.
$f1 = array(“x”, “b”, “p”, “q”, “r”, “f”);
Sort($f1)
foreach($f1 as $val)
{
echo “$val”;
}
Array Functions
asort() – sort associative array based on value
- sorts its values as sort() also preserves key
Eg.
$f1 = array(“first”=>4, “second”=>3, “third”=>1);
asort($f1)
foreach($f1 as $key=> $val)
{
echo “$val”;
}
Array Functions
ksort() – sort associative array based on key
- sorts its values as sort() also preserves key
Eg.
$f1 = array(“d”=>4, “a”=>3, “c”=>1);
asort($f1)
foreach($f1 as $key=> $val)
{
echo “$val”;
}

Más contenido relacionado

La actualidad más candente

Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP ArraysAhmed Swilam
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String FunctionsGeshan Manandhar
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Introduction to php 6
Introduction to php   6Introduction to php   6
Introduction to php 6pctechnology
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsDavid Golden
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl courseBITS
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2Heather Rock
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007Damien Seguy
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl courseMarc Logghe
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginnersleo lapworth
 

La actualidad más candente (16)

Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Array in php
Array in phpArray in php
Array in php
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
 
Array
ArrayArray
Array
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Introduction to php 6
Introduction to php   6Introduction to php   6
Introduction to php 6
 
Scripting3
Scripting3Scripting3
Scripting3
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
 
GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2GDI Seattle - Intro to JavaScript Class 2
GDI Seattle - Intro to JavaScript Class 2
 
PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007PHP and MySQL Tips and tricks, DC 2007
PHP and MySQL Tips and tricks, DC 2007
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 

Similar a PHP Arrays: A Guide to Creating, Printing and Sorting Arrays in PHP

Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hashhigaki
 
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
 
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
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in pythonJohn(Qiang) Zhang
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP ArraysTarang Desai
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby Gautam Rege
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
ABC of Perl programming
ABC of Perl programmingABC of Perl programming
ABC of Perl programmingBo Hua Yang
 
JAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfJAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfcherop41618145
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPMatheus Marabesi
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and MapsIntro C# Book
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPandrewnacin
 

Similar a PHP Arrays: A Guide to Creating, Printing and Sorting Arrays in PHP (20)

Ruby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と HashRuby初級者向けレッスン 48回 ─── Array と Hash
Ruby初級者向けレッスン 48回 ─── Array と Hash
 
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
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
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
 
Python advanced 2. regular expression in python
Python advanced 2. regular expression in pythonPython advanced 2. regular expression in python
Python advanced 2. regular expression in python
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
 
RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby RedDot Ruby Conf 2014 - Dark side of ruby
RedDot Ruby Conf 2014 - Dark side of ruby
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
ABC of Perl programming
ABC of Perl programmingABC of Perl programming
ABC of Perl programming
 
JAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdfJAVASCRIPT OBJECTS.pdf
JAVASCRIPT OBJECTS.pdf
 
Basic swift
Basic swiftBasic swift
Basic swift
 
Laravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SPLaravel collections an overview - Laravel SP
Laravel collections an overview - Laravel SP
 
Potential Friend Finder
Potential Friend FinderPotential Friend Finder
Potential Friend Finder
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
WordPress 3.1 at DC PHP
WordPress 3.1 at DC PHPWordPress 3.1 at DC PHP
WordPress 3.1 at DC PHP
 
PHP 101
PHP 101 PHP 101
PHP 101
 
Wrong
WrongWrong
Wrong
 

Más de Jalpesh Vasa

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Jalpesh Vasa
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex exampleJalpesh Vasa
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regexJalpesh Vasa
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOMJalpesh Vasa
 
2 introduction css
2 introduction css2 introduction css
2 introduction cssJalpesh Vasa
 
1 web technologies
1 web technologies1 web technologies
1 web technologiesJalpesh Vasa
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVAJalpesh Vasa
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android developmentJalpesh Vasa
 

Más de Jalpesh Vasa (16)

Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Object Oriented PHP - PART-2
Object Oriented PHP - PART-2Object Oriented PHP - PART-2
Object Oriented PHP - PART-2
 
5. HTML5
5. HTML55. HTML5
5. HTML5
 
4.4 PHP Session
4.4 PHP Session4.4 PHP Session
4.4 PHP Session
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
4 Basic PHP
4 Basic PHP4 Basic PHP
4 Basic PHP
 
3.2.1 javascript regex example
3.2.1 javascript regex example3.2.1 javascript regex example
3.2.1 javascript regex example
 
3.2 javascript regex
3.2 javascript regex3.2 javascript regex
3.2 javascript regex
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
 
3.1 javascript objects_DOM
3.1 javascript objects_DOM3.1 javascript objects_DOM
3.1 javascript objects_DOM
 
2 introduction css
2 introduction css2 introduction css
2 introduction css
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
 
Kotlin for android development
Kotlin for android developmentKotlin for android development
Kotlin for android development
 
Security in php
Security in phpSecurity in php
Security in php
 

Último

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Último (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

PHP Arrays: A Guide to Creating, Printing and Sorting Arrays in PHP

  • 2. What is Array? • Arrays are lists: list of people; list of size; list of books; • To store a group of related items in a single variable is called.... • Types of array:  Numeric array(or just an array)  Associative array  Multidimensional array  Mixed array
  • 3. What is Array? (contd.) Numeric array(or just an array) if you want to find and entry, you need to know its position(index) within an array. Eg. a[0], a[7], a[34] Associative array(also known as Hash) in this index are not integer, but strings. Eg. a*“honest”+ = (“Mahatma Gandhi”); a*“good_man”+ = (“Abraham lincoln”);
  • 4. How to create Array? You can create PHP array by using array() function. $num = array(5,4,3,2,1); $word = array(“web”, “db”, “app”); echo $num[2] = ? echo $word[1] = ?
  • 5. How to create Array? (contd.) Also array is produced by: $num[0] = 5; $num[1] = 4; $num[2] = 3; Another way is: $shopping = array(); $shopping*+ = “milk”; $shopping*+ = “coffee”; $shopping*+ = “potato”;
  • 6. How to create Array? (contd.) When you want to print an array in a double-quoted string, you need to use the braces. echo “the first item in the list is ,$shopping*0+-”; print out entire contents of an array using print_r(arr_name) function. print_r($shopping); // used only for debugging purpose
  • 7. How to create Array? (contd.) PHP array is an ordered set of variable in which each variable – called an element – has an associated key. PHP automatically assigns a key values if keys are not specified when arrays are constructed. $f*+ = “v1” $f*+ = “v2” $f*5+ = “v3” $f*+ = “v4” Keys 0 1 5 6
  • 8. How to create Array? (contd.) In associative array, you can access elements in an array by name. $abc = array( name=>”jil”, occupation=>”Business”, age=>30 ); echo $abc[age];
  • 9. How to create Array? (contd.) Also array is produced by: $abc*name+=“jil”; $abc*occupation+=“business”; $abc[age] = 30; echo $abc[age];
  • 10. How to create Array? (contd.) Multidimensional array is....array within a array. Eg. $abc = array( array(name=>”bob”, age=>25), array(name=>”alice”, age=>35), array(name=>”greg”, age=>45) ); print $abc[0][age];
  • 11. How to create Array? (contd.) $f*‘a’+ = 1; $f*‘b’+ = 2; $f*‘c’+ = 3; $f*‘d’+ = 4; $f*‘b’+ = $f*‘a’+ +$f*‘c’+ // ans is 4 $var = “the value of $f*‘b’+ = ,$f*‘b’+-”;
  • 12. How to create Array? (contd.) $a1 = array(‘a’, ‘b’, ‘c’); = array(‘a’=>1, ‘a’, ‘b’=>2, ‘b’, ‘c’=>3, ‘c’); = array(1=>“Sunday”, “Monday”, “Tuesday”); =array(‘key’=>‘value’, ‘key2’=>array(1,2,3));
  • 13. How to Print Array? To print simple array: For($i=0; $i<count($a1);$i++) { echo “the value at index $i = {$a1[$i+-”; }
  • 14. How to Print Array? <?php $my = array(“php”, “is”, “cool”); foreach($my as $k => $val) { echo “the value of index $k is:$val”; } foreach($my as $val) { echo “value:$val<br/>”; }
  • 15. Array Functions Array_merge() – joining two arrays. Eg. $f1 = array(“a”, “b”, “c”); $f2 = array(1,2,3); $f3 = array_merge($f1,$f2); foreach($f3 as $val) { echo “$val”; }
  • 16. Array Functions Array_push() – add multiple variables to array. - accepts an array & no. of further para. - returns total number of elements in the array. Eg. $f1 = array(“a”, “b”, “c”); $total = array_push($f1,1,2,3); echo “there are $total elements in $f1”; foreach($f1 as $val) { echo “$val”; }
  • 17. Array Functions Array_slice() – allows you to extract chunk of an array. - array, start position, length. - doesn’t alter the array you passed to it - returns new array containing elements Eg. $f1 = array(“a”, “b”, “c”, “d”, “e”, “f”); $f2 = array_slice($f1,2,3); foreach($f2 as $val) { echo “$val”; }
  • 18. Array Functions sort() – sort numerically indexed array based on value Rsort() – reverse sort Don’t pass associative array Eg. $f1 = array(“x”, “b”, “p”, “q”, “r”, “f”); Sort($f1) foreach($f1 as $val) { echo “$val”; }
  • 19. Array Functions asort() – sort associative array based on value - sorts its values as sort() also preserves key Eg. $f1 = array(“first”=>4, “second”=>3, “third”=>1); asort($f1) foreach($f1 as $key=> $val) { echo “$val”; }
  • 20. Array Functions ksort() – sort associative array based on key - sorts its values as sort() also preserves key Eg. $f1 = array(“d”=>4, “a”=>3, “c”=>1); asort($f1) foreach($f1 as $key=> $val) { echo “$val”; }