SlideShare a Scribd company logo
1 of 27
Understand what are arrays.
Understand php array types.
Work with array functions.
2
An array is like a special variable, which can accept more
than one value at a time.
Arrays are used for storing a collection of same type of
data.
Syntax:
$arrayname[key] = value;
3
In php there are three different kinds of arrays and each
array value is accessed using an id which is called array
index.
The following the different types of array available in
PHP.
Numeric or indexed
Associative
Multi dimensional
4
Numeric array - An array with a numeric index. Values
are and accessed in linear fashion
Arrays which store numbers, strings and any object but
their index will be represented by numeric.
By default index of an array starts from zero.
Example
$cars = array("Volvo", "BMW", "Toyota");
5
The above code creates a numeric or indexed array of strings.
It can be accessed as follows.
By giving the index position, the array elements are accessed.
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
6
Associative array - An stored array with strings as index.
This stores element values in association with key values
rather than in a strict linear index order.
In term of functionality the associative arrays are similar
to numeric arrays but they are different in terms of their
index.
7
To store the salaries of employees in an array, a
numerically indexed array would not be the best choice.
Instead, we could use the employees names as the keys
in our associative array, and the value would be their
respective salary.
Example
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
8
The above code creates an associative array. The
elements of the array’ age’ can be accessed by the key.
The following code snippet illustrates how to access the
associative array elements.
Example
$age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43";
9
The print_r() variable is used for printing the values of
an array without using looping construct.
Generally arrays will be printed with the help of looping
construct to iterate through its elements and only values
of the array elements will be printed.
But this print_r() function will print both index and
value of array elements.
10
We know that associative arrays are in the form of key-
value pair.
Loops will print only values. print_r() function will
traverse through all elements and print the entire
content with key – value pair.
But we cannot customize the output format given by
print_r() function. The option is to choose for each loop.
This ‘for each loop’ is variation of ‘for’ loop.
11
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
print_r($age);
foreach($age as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
12
Echo $array[‘joe’];
O/P : 43
$array[‘joe’]=53;
13
Multidimensional array - An array containing one or
more arrays and values are accessed using multiple
indices.
A multi-dimensional 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.
Values in the multi-dimensional array are accessed using
multiple index
14
$ students =array(
array(“john”, 21, “Male”);
array(“sakshi”, 20, “Female”);
);
Echo $students[1][1];
o/p: 20
Remember : The index value always starts from 0.
15
PHP provides pre defined functions which are useful for
working with arrays.
The following are some of the functions which are related to
arrays.
array() – creates an array.
array_merge() – Merges one or more arrays into one array.
array_pop() – Deletes the last element of an array
aray_push() - Inserts one or more elements to the end of an array
count() - Returns the length of an array.
16
Count()
$fruits = [‘apple’, ‘banana’,
‘orange’];
$count= count($fruits);
Echo $count; //o/p:3
17
Array-push() - Adds one or more
elements to the end of an array.
$stack = [‘a’, ‘b’];
Array_push($stack, ‘c’, ‘d’);
echo $stack;
//o/p: a, b, c, d
18
$array = [1, 2, 3];
$lastElement = array_pop($array);
// $lastElement is 3,
$array is now [1, 2]
19
$array = [1, 2, 3];
$firstElement = array_shift($array);
// $firstElement is 1,
$array is now [2, 3]
20
$array = [1, 2, 3];
array_unshift($array, 0);
// $array is now [0, 1, 2, 3]
21
$stack = [‘a’, ‘b’];
$tack=[‘c’,’d’];
$mergearray=array_merge(
$stack, $tack);
22
$stack = [‘a’, ‘b’];
$reverse=array_reverse($stack);
Echo $stack;
23
$fruits = [‘apple’, ‘banana’, ‘ kiwi’];
$isbananaExists= in_array(‘banana’, $fruits);
if($isbananaExists)
Echo“ Banana is inside the list”;
else
Echo“ Banana is not in the list”;
24
Arrays are used for storing a collection of same type of
data.
The following the different types of array available in
PHP.
Numeric or indexed
Associative
Multi dimensional
25
PHP provides predefined functions for array
manipulations.
To iterate the array elements we can use print_r()
functions, for loop and foe each loop.
26
27

More Related Content

Similar to Unit 2-Arrays.pptx

Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 
Array in php
Array in phpArray in php
Array in phpilakkiya
 
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 ...Dhivyaa C.R
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.pptAqeelAbbas94
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 

Similar to Unit 2-Arrays.pptx (20)

[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Array lecture
Array lectureArray lecture
Array lecture
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 
Array in php
Array in phpArray in php
Array in php
 
02 arrays
02 arrays02 arrays
02 arrays
 
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
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt19-Lec - Multidimensional Arrays.ppt
19-Lec - Multidimensional Arrays.ppt
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 

Recently uploaded

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Recently uploaded (20)

KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 

Unit 2-Arrays.pptx

  • 1.
  • 2. Understand what are arrays. Understand php array types. Work with array functions. 2
  • 3. An array is like a special variable, which can accept more than one value at a time. Arrays are used for storing a collection of same type of data. Syntax: $arrayname[key] = value; 3
  • 4. In php there are three different kinds of arrays and each array value is accessed using an id which is called array index. The following the different types of array available in PHP. Numeric or indexed Associative Multi dimensional 4
  • 5. Numeric array - An array with a numeric index. Values are and accessed in linear fashion Arrays which store numbers, strings and any object but their index will be represented by numeric. By default index of an array starts from zero. Example $cars = array("Volvo", "BMW", "Toyota"); 5
  • 6. The above code creates a numeric or indexed array of strings. It can be accessed as follows. By giving the index position, the array elements are accessed. $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; 6
  • 7. Associative array - An stored array with strings as index. This stores element values in association with key values rather than in a strict linear index order. In term of functionality the associative arrays are similar to numeric arrays but they are different in terms of their index. 7
  • 8. To store the salaries of employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary. Example $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); 8
  • 9. The above code creates an associative array. The elements of the array’ age’ can be accessed by the key. The following code snippet illustrates how to access the associative array elements. Example $age['Peter'] = "35"; $age['Ben'] = "37"; $age['Joe'] = "43"; 9
  • 10. The print_r() variable is used for printing the values of an array without using looping construct. Generally arrays will be printed with the help of looping construct to iterate through its elements and only values of the array elements will be printed. But this print_r() function will print both index and value of array elements. 10
  • 11. We know that associative arrays are in the form of key- value pair. Loops will print only values. print_r() function will traverse through all elements and print the entire content with key – value pair. But we cannot customize the output format given by print_r() function. The option is to choose for each loop. This ‘for each loop’ is variation of ‘for’ loop. 11
  • 12. Example <?php $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43"); print_r($age); foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "<br>"; } ?> 12
  • 13. Echo $array[‘joe’]; O/P : 43 $array[‘joe’]=53; 13
  • 14. Multidimensional array - An array containing one or more arrays and values are accessed using multiple indices. A multi-dimensional 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. Values in the multi-dimensional array are accessed using multiple index 14
  • 15. $ students =array( array(“john”, 21, “Male”); array(“sakshi”, 20, “Female”); ); Echo $students[1][1]; o/p: 20 Remember : The index value always starts from 0. 15
  • 16. PHP provides pre defined functions which are useful for working with arrays. The following are some of the functions which are related to arrays. array() – creates an array. array_merge() – Merges one or more arrays into one array. array_pop() – Deletes the last element of an array aray_push() - Inserts one or more elements to the end of an array count() - Returns the length of an array. 16
  • 17. Count() $fruits = [‘apple’, ‘banana’, ‘orange’]; $count= count($fruits); Echo $count; //o/p:3 17
  • 18. Array-push() - Adds one or more elements to the end of an array. $stack = [‘a’, ‘b’]; Array_push($stack, ‘c’, ‘d’); echo $stack; //o/p: a, b, c, d 18
  • 19. $array = [1, 2, 3]; $lastElement = array_pop($array); // $lastElement is 3, $array is now [1, 2] 19
  • 20. $array = [1, 2, 3]; $firstElement = array_shift($array); // $firstElement is 1, $array is now [2, 3] 20
  • 21. $array = [1, 2, 3]; array_unshift($array, 0); // $array is now [0, 1, 2, 3] 21
  • 22. $stack = [‘a’, ‘b’]; $tack=[‘c’,’d’]; $mergearray=array_merge( $stack, $tack); 22
  • 23. $stack = [‘a’, ‘b’]; $reverse=array_reverse($stack); Echo $stack; 23
  • 24. $fruits = [‘apple’, ‘banana’, ‘ kiwi’]; $isbananaExists= in_array(‘banana’, $fruits); if($isbananaExists) Echo“ Banana is inside the list”; else Echo“ Banana is not in the list”; 24
  • 25. Arrays are used for storing a collection of same type of data. The following the different types of array available in PHP. Numeric or indexed Associative Multi dimensional 25
  • 26. PHP provides predefined functions for array manipulations. To iterate the array elements we can use print_r() functions, for loop and foe each loop. 26
  • 27. 27