SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Arrays
Arrays
Indexed Array
int a[]={1,2,3,4,5,6,7,8,9,10};
for(i=0;i<10;i++)
Printf(‚%d‛,a[i]);
Indexed Array / Enumerated array
$a=array(1,2,3,4,5,6,7,8,9,10)
for($i=0;$i<10;$i++)
echo $a[$i] ;
Associative Array
$a*‘name’+=‚John‛;
$a*‘age’+=24;
$a*‘mark’+=35.65;
Foreach($a as $key=>$value)
echo ‚ $key contains $ value‛;
C PHP
Printing Arrays
• PHP provides two functions that can be used to output a variable’s
value recursively
• print_r()
• var_dump().
Eg:
$a=array(12,13,’baabtra’’);
Var_dump($a);
Print_r($a);
//array(3) { [0]=> int(12) [1]=> int(13) [2]=> string(7) "baabtra‛}
// Array ( [0] => 12 [1] => 13 [2] => baabtra)
Arrays
• Outputs only the value and not the
datatype
• Cannot out put multiple varaiables at a
time
• Print_r returns upon printing
something
Var_dump Print_r
• outputs the data types of each value
• is capable of outputting the value of
more than one variable at the same time
•Doesn’t return anything
Multi Dimensional Arrays
To create multi-dimensional arrays, we simply assign an array as the
value for an array element
$a = array();
$a*+ = array(’foo’,’bar’);
$a*+ = array(’baz’,’bat’);
echo $a[0][1] . $a[1][0]; // outputa barbaz.
Array iterations
$array = array(’foo’, ’bar’, ’baz’);
foreach ($array as $key => $value) {
echo "$key: $value";
}
$a = array (1, 2, 3);
foreach ($a as $k => &$v) {
$v += 1;
}
var_dump ($a);
0 : foo
1 : bar
2 : baz
array(6) {
[0]=> int(2)
[1]=> int(3)
[2]=> int(4)
}
Out put
Out put
Unravelling Arrays
• It is sometimes simpler to work with the values of an array by
assigning them to individual variables. we do this by calling a
function named list()
• Eg:
$myarray = array (1, 2, 3);
list($a,$b,$c)=$myarray;
echo $b; //outputs 2
Array operations
• Array Union
$a = array (1, 2, 3);
$b = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a + $b);
‚Here the result includes all of the elements of the two original arrays, even though
they have the same values; this is a result of the fact that the keys are different‛
array(6) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
["a"]=> int(1)
["b"]=> int(2)
["c"]=> int(3)
}
Out put
Array operations
$a = array (1, 2, 3);
$b = array (’a’ => 1, 2, 3);
var_dump ($a + $b);
‚if the two arrays have common elements that also share the same
keys, they would only appear once in the end result:‛
array(4) {
[0]=>int(1)
[1]=>int(2)
[2]=>int(3)
["a"]=>int(1)
}
Out put
Comparing Arrays == and ===
$a = array (1, 2, 3);
$b = array (1 => 2, 2 => 3, 0 => 1);
$c = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a == $b); // True
var_dump ($a === $b); // False
var_dump ($a == $c); // True
var_dump ($a === $c); // False
Comparing Arrays
$a = array (1, 2, 3);
$b = array (1 => 2, 2 => 3, 0 => 1);
$c = array (’a’ => 1, ’b’ => 2, ’c’ => 3);
var_dump ($a == $b); // True
var_dump ($a === $b); // False
var_dump ($a == $c); // True
var_dump ($a === $c); // False
As you can see, the equivalence operator == returns
true if both arrays have the same number of
elements with the same values and keys, regardless
of their order. The identity operator ===, on the
other hand, returns true only if the array contains
the same key/value pairs in the same order.
Array Functions
Array functions – count()
Count() returns the number of elements in an array
$a = array (1, 2, 4);
$b = array();
$c = 10;
echo count ($a); // Outputs 3
echo count ($b); // Outputs 0
echo count ($c); // Outputs 1 . ie count() cannot be used to determine whether
a variable contains an array or not
Array functions – is_array()
is_array() returns True if a variable contains array or else False
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
$b=12 ;
• echo count ($a); // Outputs 6
• echo count($b); // outputs 1
• echo is_array($b) //outputs false
Array functions – isset()
• isset() used to determine whether an element with the given key
exists or not.
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
echo isset ($a*’a’+); // True
echo isset ($a*’c’+); // False
isset() has the major drawback of considering an element whose value is NULL
Array functions – array_key_exists()
array_key_exists() used to determine whether an element exists
within an array or not
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
$b=12 ;
echo array_key_exists ($a*’a’+); // True
echo array_key_exists ($a*’c’+); // True
Array functions – in_array()
in_array() used to determine whether a value exists within an array
or not
$a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ;
$b=12 ;
echo in_array ($a, 2); // True
Array functions- array_flip()
• array_flip() inverts the value of each element of an array with its
key
$a = array (’a’, ’b’, ’c’);
var_dump (array_flip ($a));
array(3) {
["a"]=>int(0)
["b"]=>int(1)
["c"]=>int(2)
}
Out put
Array functions- array_reverse()
• array_reverse() inverts the order of the array’s elements,so that
the last one appears first:
$a = array (’x’ => ’a’, 10 => ’b’, ’c’);
var_dump (array_reverse ($a));
array(3) {
[0]=>string(1) "c"
[1]=>string(1) "b"
["x"]=>string(1) "a"
}
Out put
Array functions – sort()
• sort() sorts an array based on its values
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
sort($array);
var_dump($array);
• ‚sort() effectively destroys all the keys in the array and renumbers its
elements‛
array(3) {
[0]=>string(3) "bar"
[1]=>string(3) "baz"
[2]=>string(3) "foo"
}
Out put
Array functions –asort()
• asort() :If you wish to maintain key association, you can use asort()
instead of sort()
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
asort($array);
var_dump($array);
array(3) {
["b"]=>string(3) "bar"
["c"]=>string(3) "baz"
["a"]=>string(3) "foo"
}
Out put
Parameters for sort() an asort()
• Both sort() and asort() accept a second, optional parameter that allows
you to specify how the sort operation takes place
• SORT_REGULAR : Compare items as they appear in the array, without
performing any kind of conversion. This is the default behaviour.
• SORT_NUMERIC : Convert each element to a numeric value for sorting
purposes.
• SORT_STRING : Compare all elements as strings.
Array functions –rsort() and arsort()
Both sort() and asort() sort values in ascending order. To sort them in
descending order, you can use rsort() and arsort().
$array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’);
arsort($array);
var_dump($array);
array(3) {
["a"]=>string(3) "foo"
["c"]=>string(3) "baz"
*"b"+=>string(3) "bar“
}
Out put
Array functions –natsort()
The sorting operation performed by sort() and asort() simply takes into consideration either
the numeric value of each element, or performs a byte-by-byte comparison of strings values.
This can result in an ‚unnatural‛ sorting order—for example, the string value ’10t’ will be
considered ‚lower‛ than ’2t’ because it starts with the character 1, which has a lower value
than 2. If this sorting algorithm doesn’t work well for your needs, you can try using
natsort() instead:
$array = array(’10t’, ’2t’, ’3t’);
natsort($array);
var_dump($array)
array(3) {
[1]=>
string(2) "2t"
[2]=>
string(2) "3t"
[0]=>
string(3) "10t"
}
Out put
Array functions –ksort() and krsort()
• PHP allows you to sort by key (rather than by value) using the
ksort() and krsort() functions, which work analogously to sort() and
rsort():
$a = array (’a’ => 30, ’b’ => 10, ’c’ => 22);
ksort($a);
var_dump ($a);
array(3) {
["a"]=>int(30)
["b"]=>int(10)
["c"]=>int(22)
}
Out put
Array functions –shuffle()
There are circumstances where, instead of ordering an array, you will want to
scramble its contents so that the keys are randomized; this can be done by
using the shuffle() function:
$cards = array (1, 2, 3, 4);
shuffle ($cards);
var_dump ($cards);
array(9) {
[0]=>int(4)
[1]=>int(1)
[2]=>int(2)
[3]=>int(3)
}
Out put
Array functions –array_rand()
If you need to extract individual elements from the array at random, you can
use array_rand(), which returns one or more random keys from an array:
$cards = array (’a’ => 10, ’b’ => 12, ’c’ => 13);
$keys = array_rand ($cards, 2);
var_dump ($keys);
var_dump ($cards);
array(2) {
[0]=>string(1) "a"
[1]=>string(1) "b"
}
array(3) {
["a"]=>int(10)
["b"]=>int(12)
["c"]=>int(13)
}
Out put
Array functions –array_diff()
array_diff() is used to compute the difference between two arrays:
$a = array (1, 2, 3);
$b = array (1, 3, 4);
var_dump (array_diff ($a, $b));
array(1) {
[1]=>
int(2)
}
Out put
Questions?
‚A good question deserve a good grade…‛
Self Check !!
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com

Más contenido relacionado

La actualidad más candente

Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
PROIDEA
 

La actualidad más candente (20)

Functional programming with php7
Functional programming with php7Functional programming with php7
Functional programming with php7
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)
 
Php array
Php arrayPhp array
Php array
 
PHP array 1
PHP array 1PHP array 1
PHP array 1
 
PHP 101
PHP 101 PHP 101
PHP 101
 
Modern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter BootstrapModern Application Foundations: Underscore and Twitter Bootstrap
Modern Application Foundations: Underscore and Twitter Bootstrap
 
Frege is a Haskell for the JVM
Frege is a Haskell for the JVMFrege is a Haskell for the JVM
Frege is a Haskell for the JVM
 
Personal Perl 6 compiler
Personal Perl 6 compilerPersonal Perl 6 compiler
Personal Perl 6 compiler
 
Useful functions for arrays in php
Useful functions for arrays in phpUseful functions for arrays in php
Useful functions for arrays in php
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
JDD2015: Functional programing and Event Sourcing - a pair made in heaven - e...
 
Intro
IntroIntro
Intro
 
The Art of Transduction
The Art of TransductionThe Art of Transduction
The Art of Transduction
 
Pybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonPybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in Python
 
Damn Fine CoffeeScript
Damn Fine CoffeeScriptDamn Fine CoffeeScript
Damn Fine CoffeeScript
 
List of all php array functions
List of all php array functionsList of all php array functions
List of all php array functions
 
Comparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statementsComparing 30 MongoDB operations with Oracle SQL statements
Comparing 30 MongoDB operations with Oracle SQL statements
 

Similar a Intoduction to php arrays

Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
PrinceGuru MS
 
PHP record- with all programs and output
PHP record- with all programs and outputPHP record- with all programs and output
PHP record- with all programs and output
KavithaK23
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 

Similar a Intoduction to php arrays (20)

PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
Array Methods.pptx
Array Methods.pptxArray Methods.pptx
Array Methods.pptx
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks
 
PHP record- with all programs and output
PHP record- with all programs and outputPHP record- with all programs and output
PHP record- with all programs and output
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Groovy collection api
Groovy collection apiGroovy collection api
Groovy collection api
 
Scripting3
Scripting3Scripting3
Scripting3
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Php functions
Php functionsPhp functions
Php functions
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
Perl6 a whistle stop tour
Perl6 a whistle stop tourPerl6 a whistle stop tour
Perl6 a whistle stop tour
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
What are arrays in java script
What are arrays in java scriptWhat are arrays in java script
What are arrays in java script
 

Más de baabtra.com - No. 1 supplier of quality freshers

Más de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Intoduction to php arrays

  • 2. Arrays Indexed Array int a[]={1,2,3,4,5,6,7,8,9,10}; for(i=0;i<10;i++) Printf(‚%d‛,a[i]); Indexed Array / Enumerated array $a=array(1,2,3,4,5,6,7,8,9,10) for($i=0;$i<10;$i++) echo $a[$i] ; Associative Array $a*‘name’+=‚John‛; $a*‘age’+=24; $a*‘mark’+=35.65; Foreach($a as $key=>$value) echo ‚ $key contains $ value‛; C PHP
  • 3. Printing Arrays • PHP provides two functions that can be used to output a variable’s value recursively • print_r() • var_dump(). Eg: $a=array(12,13,’baabtra’’); Var_dump($a); Print_r($a); //array(3) { [0]=> int(12) [1]=> int(13) [2]=> string(7) "baabtra‛} // Array ( [0] => 12 [1] => 13 [2] => baabtra)
  • 4. Arrays • Outputs only the value and not the datatype • Cannot out put multiple varaiables at a time • Print_r returns upon printing something Var_dump Print_r • outputs the data types of each value • is capable of outputting the value of more than one variable at the same time •Doesn’t return anything
  • 5. Multi Dimensional Arrays To create multi-dimensional arrays, we simply assign an array as the value for an array element $a = array(); $a*+ = array(’foo’,’bar’); $a*+ = array(’baz’,’bat’); echo $a[0][1] . $a[1][0]; // outputa barbaz.
  • 6. Array iterations $array = array(’foo’, ’bar’, ’baz’); foreach ($array as $key => $value) { echo "$key: $value"; } $a = array (1, 2, 3); foreach ($a as $k => &$v) { $v += 1; } var_dump ($a); 0 : foo 1 : bar 2 : baz array(6) { [0]=> int(2) [1]=> int(3) [2]=> int(4) } Out put Out put
  • 7. Unravelling Arrays • It is sometimes simpler to work with the values of an array by assigning them to individual variables. we do this by calling a function named list() • Eg: $myarray = array (1, 2, 3); list($a,$b,$c)=$myarray; echo $b; //outputs 2
  • 8. Array operations • Array Union $a = array (1, 2, 3); $b = array (’a’ => 1, ’b’ => 2, ’c’ => 3); var_dump ($a + $b); ‚Here the result includes all of the elements of the two original arrays, even though they have the same values; this is a result of the fact that the keys are different‛ array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } Out put
  • 9. Array operations $a = array (1, 2, 3); $b = array (’a’ => 1, 2, 3); var_dump ($a + $b); ‚if the two arrays have common elements that also share the same keys, they would only appear once in the end result:‛ array(4) { [0]=>int(1) [1]=>int(2) [2]=>int(3) ["a"]=>int(1) } Out put
  • 10. Comparing Arrays == and === $a = array (1, 2, 3); $b = array (1 => 2, 2 => 3, 0 => 1); $c = array (’a’ => 1, ’b’ => 2, ’c’ => 3); var_dump ($a == $b); // True var_dump ($a === $b); // False var_dump ($a == $c); // True var_dump ($a === $c); // False
  • 11. Comparing Arrays $a = array (1, 2, 3); $b = array (1 => 2, 2 => 3, 0 => 1); $c = array (’a’ => 1, ’b’ => 2, ’c’ => 3); var_dump ($a == $b); // True var_dump ($a === $b); // False var_dump ($a == $c); // True var_dump ($a === $c); // False As you can see, the equivalence operator == returns true if both arrays have the same number of elements with the same values and keys, regardless of their order. The identity operator ===, on the other hand, returns true only if the array contains the same key/value pairs in the same order.
  • 13. Array functions – count() Count() returns the number of elements in an array $a = array (1, 2, 4); $b = array(); $c = 10; echo count ($a); // Outputs 3 echo count ($b); // Outputs 0 echo count ($c); // Outputs 1 . ie count() cannot be used to determine whether a variable contains an array or not
  • 14. Array functions – is_array() is_array() returns True if a variable contains array or else False $a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ; $b=12 ; • echo count ($a); // Outputs 6 • echo count($b); // outputs 1 • echo is_array($b) //outputs false
  • 15. Array functions – isset() • isset() used to determine whether an element with the given key exists or not. $a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ; echo isset ($a*’a’+); // True echo isset ($a*’c’+); // False isset() has the major drawback of considering an element whose value is NULL
  • 16. Array functions – array_key_exists() array_key_exists() used to determine whether an element exists within an array or not $a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ; $b=12 ; echo array_key_exists ($a*’a’+); // True echo array_key_exists ($a*’c’+); // True
  • 17. Array functions – in_array() in_array() used to determine whether a value exists within an array or not $a = array (1, 2, 4,’a’=>10,’b’=>12,’c’=>null) ; $b=12 ; echo in_array ($a, 2); // True
  • 18. Array functions- array_flip() • array_flip() inverts the value of each element of an array with its key $a = array (’a’, ’b’, ’c’); var_dump (array_flip ($a)); array(3) { ["a"]=>int(0) ["b"]=>int(1) ["c"]=>int(2) } Out put
  • 19. Array functions- array_reverse() • array_reverse() inverts the order of the array’s elements,so that the last one appears first: $a = array (’x’ => ’a’, 10 => ’b’, ’c’); var_dump (array_reverse ($a)); array(3) { [0]=>string(1) "c" [1]=>string(1) "b" ["x"]=>string(1) "a" } Out put
  • 20. Array functions – sort() • sort() sorts an array based on its values $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’); sort($array); var_dump($array); • ‚sort() effectively destroys all the keys in the array and renumbers its elements‛ array(3) { [0]=>string(3) "bar" [1]=>string(3) "baz" [2]=>string(3) "foo" } Out put
  • 21. Array functions –asort() • asort() :If you wish to maintain key association, you can use asort() instead of sort() $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’); asort($array); var_dump($array); array(3) { ["b"]=>string(3) "bar" ["c"]=>string(3) "baz" ["a"]=>string(3) "foo" } Out put
  • 22. Parameters for sort() an asort() • Both sort() and asort() accept a second, optional parameter that allows you to specify how the sort operation takes place • SORT_REGULAR : Compare items as they appear in the array, without performing any kind of conversion. This is the default behaviour. • SORT_NUMERIC : Convert each element to a numeric value for sorting purposes. • SORT_STRING : Compare all elements as strings.
  • 23. Array functions –rsort() and arsort() Both sort() and asort() sort values in ascending order. To sort them in descending order, you can use rsort() and arsort(). $array = array(’a’ => ’foo’, ’b’ => ’bar’, ’c’ => ’baz’); arsort($array); var_dump($array); array(3) { ["a"]=>string(3) "foo" ["c"]=>string(3) "baz" *"b"+=>string(3) "bar“ } Out put
  • 24. Array functions –natsort() The sorting operation performed by sort() and asort() simply takes into consideration either the numeric value of each element, or performs a byte-by-byte comparison of strings values. This can result in an ‚unnatural‛ sorting order—for example, the string value ’10t’ will be considered ‚lower‛ than ’2t’ because it starts with the character 1, which has a lower value than 2. If this sorting algorithm doesn’t work well for your needs, you can try using natsort() instead: $array = array(’10t’, ’2t’, ’3t’); natsort($array); var_dump($array) array(3) { [1]=> string(2) "2t" [2]=> string(2) "3t" [0]=> string(3) "10t" } Out put
  • 25. Array functions –ksort() and krsort() • PHP allows you to sort by key (rather than by value) using the ksort() and krsort() functions, which work analogously to sort() and rsort(): $a = array (’a’ => 30, ’b’ => 10, ’c’ => 22); ksort($a); var_dump ($a); array(3) { ["a"]=>int(30) ["b"]=>int(10) ["c"]=>int(22) } Out put
  • 26. Array functions –shuffle() There are circumstances where, instead of ordering an array, you will want to scramble its contents so that the keys are randomized; this can be done by using the shuffle() function: $cards = array (1, 2, 3, 4); shuffle ($cards); var_dump ($cards); array(9) { [0]=>int(4) [1]=>int(1) [2]=>int(2) [3]=>int(3) } Out put
  • 27. Array functions –array_rand() If you need to extract individual elements from the array at random, you can use array_rand(), which returns one or more random keys from an array: $cards = array (’a’ => 10, ’b’ => 12, ’c’ => 13); $keys = array_rand ($cards, 2); var_dump ($keys); var_dump ($cards); array(2) { [0]=>string(1) "a" [1]=>string(1) "b" } array(3) { ["a"]=>int(10) ["b"]=>int(12) ["c"]=>int(13) } Out put
  • 28. Array functions –array_diff() array_diff() is used to compute the difference between two arrays: $a = array (1, 2, 3); $b = array (1, 3, 4); var_dump (array_diff ($a, $b)); array(1) { [1]=> int(2) } Out put
  • 29. Questions? ‚A good question deserve a good grade…‛
  • 31. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • 32. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com