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”;
}

More Related Content

PPTX
Chap 3php array part1
PDF
Php array
PDF
Arrays in PHP
PPTX
PHP array 1
PPT
Php array
PDF
PHP Unit 4 arrays
PPT
Php Using Arrays
PPTX
Array in php
Chap 3php array part1
Php array
Arrays in PHP
PHP array 1
Php array
PHP Unit 4 arrays
Php Using Arrays
Array in php

What's hot (16)

PPT
PPT
Class 4 - PHP Arrays
PPTX
Array in php
PPT
03 Php Array String Functions
PPTX
Array
PDF
Array String - Web Programming
PPTX
Introduction to php 6
PDF
Scripting3
PDF
Taking Perl to Eleven with Higher-Order Functions
PPT
PHP array 2
PPTX
Marcs (bio)perl course
PDF
GDI Seattle - Intro to JavaScript Class 2
PDF
PHP and MySQL Tips and tricks, DC 2007
PDF
Marc’s (bio)perl course
PDF
DBIx::Class beginners
Class 4 - PHP Arrays
Array in php
03 Php Array String Functions
Array
Array String - Web Programming
Introduction to php 6
Scripting3
Taking Perl to Eleven with Higher-Order Functions
PHP array 2
Marcs (bio)perl course
GDI Seattle - Intro to JavaScript Class 2
PHP and MySQL Tips and tricks, DC 2007
Marc’s (bio)perl course
DBIx::Class beginners
Ad

Similar to 4.1 PHP Arrays (20)

PPT
Web Technology - PHP Arrays
PPTX
Chapter 2 wbp.pptx
PPTX
Unit 2-Arrays.pptx
PPTX
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPT
PHP-04-Arrays.ppt
PPTX
Arrays in PHP
PPTX
Php arrays
DOCX
Array andfunction
PPTX
Arrays syntax and it's functions in php.pptx
PPT
PHP - Introduction to PHP Arrays
PPTX
PHP Arrays_Introduction
PPTX
Web Application Development using PHP Chapter 4
PPT
Arrays in php
PPTX
Arrays in php
PPT
Chapter 04 array
PDF
PHP Basic & Arrays
PPT
PHP and MySQL with snapshots
PDF
PHP tips and tricks
PPT
Php course-in-navimumbai
Web Technology - PHP Arrays
Chapter 2 wbp.pptx
Unit 2-Arrays.pptx
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PHP-04-Arrays.ppt
Arrays in PHP
Php arrays
Array andfunction
Arrays syntax and it's functions in php.pptx
PHP - Introduction to PHP Arrays
PHP Arrays_Introduction
Web Application Development using PHP Chapter 4
Arrays in php
Arrays in php
Chapter 04 array
PHP Basic & Arrays
PHP and MySQL with snapshots
PHP tips and tricks
Php course-in-navimumbai
Ad

More from Jalpesh Vasa (16)

PDF
Object Oriented PHP - PART-1
PDF
Object Oriented PHP - PART-2
PDF
5. HTML5
PDF
4.4 PHP Session
PDF
4.3 MySQL + PHP
PDF
4.2 PHP Function
PDF
4 Basic PHP
PDF
3.2.1 javascript regex example
PDF
3.2 javascript regex
PDF
3. Java Script
PDF
3.1 javascript objects_DOM
PDF
2 introduction css
PDF
1 web technologies
PDF
Remote Method Invocation in JAVA
PDF
Kotlin for android development
PDF
Security in php
Object Oriented PHP - PART-1
Object Oriented PHP - PART-2
5. HTML5
4.4 PHP Session
4.3 MySQL + PHP
4.2 PHP Function
4 Basic PHP
3.2.1 javascript regex example
3.2 javascript regex
3. Java Script
3.1 javascript objects_DOM
2 introduction css
1 web technologies
Remote Method Invocation in JAVA
Kotlin for android development
Security in php

Recently uploaded (20)

PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
Laparoscopic Colorectal Surgery at WLH Hospital
PDF
Health aspects of bilberry: A review on its general benefits
PPTX
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
PPTX
Climate Change and Its Global Impact.pptx
PDF
Chevening Scholarship Application and Interview Preparation Guide
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
PUBH1000 - Module 6: Global Health Tute Slides
PDF
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
PDF
faiz-khans about Radiotherapy Physics-02.pdf
PDF
African Communication Research: A review
PDF
Disorder of Endocrine system (1).pdfyyhyyyy
PDF
Diabetes Mellitus , types , clinical picture, investigation and managment
PPTX
Diploma pharmaceutics notes..helps diploma students
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
4. Diagnosis and treatment planning in RPD.pptx
PDF
The TKT Course. Modules 1, 2, 3.for self study
PPTX
Macbeth play - analysis .pptx english lit
PPTX
pharmaceutics-1unit-1-221214121936-550b56aa.pptx
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
Laparoscopic Colorectal Surgery at WLH Hospital
Health aspects of bilberry: A review on its general benefits
Cite It Right: A Compact Illustration of APA 7th Edition.pptx
Climate Change and Its Global Impact.pptx
Chevening Scholarship Application and Interview Preparation Guide
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PUBH1000 - Module 6: Global Health Tute Slides
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
faiz-khans about Radiotherapy Physics-02.pdf
African Communication Research: A review
Disorder of Endocrine system (1).pdfyyhyyyy
Diabetes Mellitus , types , clinical picture, investigation and managment
Diploma pharmaceutics notes..helps diploma students
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
4. Diagnosis and treatment planning in RPD.pptx
The TKT Course. Modules 1, 2, 3.for self study
Macbeth play - analysis .pptx english lit
pharmaceutics-1unit-1-221214121936-550b56aa.pptx

4.1 PHP Arrays

  • 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”; }