SlideShare a Scribd company logo
PHP Arrays An  indexed  array is similar to one provided by a conventional programming language. An element  of an  associative  array can be accessed by a keyword.  An associative array is like a  dictionary  or  map . An array element can be of any type.  An array can be  heterogeneous with its element types and structure. Many functions manipulating an array are provided. PHP Array
Indexed Array PHP Array $animals =  array("dog", "cat", "fish"); echo "$animals[0]\n"; echo "$animals[2]\n"; echo "$animals\n"; dog fish Array
Updating and Adding Elements PHP Array $animals =  array("dog", "cat", "fish"); echo "$animals[1]\n";  $animals[1] = " tiger " ;   echo "$animals[1]\n"; $animals[] = "beaver"; echo "$animals[3]\n"; cat tiger beaver
Associative Array PHP Array $animals = array(  "dog“ => 15,"cat“ = >8, "fish“ => 2); echo "$animals[cat]\n"; $animals["bat"] = 100; echo "$animals[bat]\n";  8 100
Listing array element :  for PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); for ($i = 0; $i < count($animals); $i++) { echo $i . &quot;-th animal is a $animals[$i].\n&quot;; } 0-th animal is a dog. 1-th animal is a cat. 2-th animal is a fish.
Listing Array Elements:  foreach PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); foreach ($animals as $animal)  echo &quot;$animal\n&quot;; } dog cat fish
while  and  each PHP Array $animals = array(  &quot;dog“ => 15,&quot;cat“ => 8, &quot;fish“ => 2); while ($item = each($animals))  print &quot;weight of &quot; .  $item[&quot;key&quot;] . &quot; is &quot; . $item[&quot;value&quot;] . “.\n&quot;; weight of dog is 15. weight of cat is 8. weight of fish is 2.
each  and  list PHP Array $animals = array(  &quot;dog“ => 15, &quot;cat“ => 8, &quot;fish“ => 2); while (list($key, $value) =  each($animals)) print &quot;weight of $key is $value.\n&quot;; weight of dog is 15. weight of cat is 8. weight of fish is 2.
Multi-Dimensional Heterogeneous Array PHP Array $books = array( array(&quot;title“  => “A&quot;, &quot;author“ => “X&quot;), array(&quot;title“  => “B&quot;, “ author“ => “Y&quot;, “ price“  => 25) ); print_r($books); Array ( [0] => Array ( [title]  => A [author] => X  ) [1] => Array ( [title]  => B [author] => Y [price]  => 25 ) )
Nested Loops PHP Array for ($i=0; $i < count($books); $i++) { print &quot;$i-th book is:&quot;; while ( list($key, $value) =  each($books[$i]) ) print “ $key: $value&quot;; print &quot;\n&quot;; } 0-th book is: title: A author: X 1-th book is: title: B author: Y price: 25
String as an Array PHP Array $myString = &quot;My chars&quot;; echo &quot;$myString\n&quot;;  echo &quot;$myString[1]\n&quot;;  My chars y
Array functions count(), sizeof()  in_array() array_slice ()  array_pad() array_reverse() list( ) Sorting Functions   sort(), rsort() asort(), arsort() ksort(), krsort() PHP Array
count(array1) and  sizeof(array1) PHP Array Returns the size of  array  array1 . $animals = array ('dog', 'cat', 'fish'); echo count($animals); echo sizeof($animals); 3 3
array array_pad(array1, length, value) PHP Array Pad array to length  length  with  value . $scores =  array(1, 2); $padded =  array_pad($scores, 5, 10); print_r($padded); Array  ( [0] => 1 [1] => 2 [2] => 10 [3] => 10 [4] => 10 )
array array_reverse(array1) PHP Array Return an array with elements in reverse order. Array  ( [0] => fish [1] => cat [2] => dog ) $animals =  array('dog', 'cat', 'fish'); $reversed  array_reverse($animals); print_r($reversed);
array array_slice   (array1, offset, length) PHP Array Extract a slice of  length  from  array1  starting at offset . $array1 =  array(1, 2, 3, 4, 5, 6); $subarray =  array_slice(array1, 3, 2); print_r($subarray); Array ( [0] => 4 [1] => 5 )
boolean in_array(value, array1) PHP Array Check if  value  exists in  array1 . $animals = array('dog', 'cat', 'fish'); echo in_array('cat', $animals); echo in_array(‘monkey', $animals); 1 (true) (false)
void list(var1, var2, ...) PHP Array The elements of an array are copied into the list of variables  var1, var2,  . . . $animals = array('dog', 'cat', 'fish'); list($a, $b, $c) = $animals; echo &quot;$a, $b, $c&quot;; dog, cat, fish
sort(array) and  rsort(array) PHP Array Sort  the elements in an array in increasing or decreasing order. $animals =  array('dog', 'cat', fish'); sort($animals); print_r($animals); Array ( [0] => cat [1] => dog [2] => fish  )
asort(array), arsort(array) PHP Array Sort  an array maintaining index association. $animals =  array('dog', 'cat', 'fish'); asort($animals); print_r($animals); Array ( [1] => cat [0] => dog [2] => fish  )
ksort(array), krsort(array) PHP Array Sort  array by keys. $animals =  array('dog' => 15,  'cat' => 8,  'fish' => 2); ksort($animals); print_r($animals); Array ( [cat] => 8 [dog] => 15 [fish] => 12 )

More Related Content

What's hot (20)

PPT
Introduction to Python
amiable_indian
 
PPTX
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
PDF
Binary Search Tree and Heap in Prolog
Benjamin Guillet
 
PPTX
List in Python
Sharath Ankrajegowda
 
PPTX
arrays in c
vidhi mehta
 
PPTX
Python basics
RANAALIMAJEEDRAJPUT
 
PPTX
php basics
Anmol Paul
 
PDF
CNS - Unit - 2 - Stream Ciphers and Block Ciphers
Gyanmanjari Institute Of Technology
 
PDF
PythonOOP
Veera Pendyala
 
PPTX
Lecture 02 lexical analysis
Iffat Anjum
 
PDF
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
PPTX
Structure of the compiler
Sudhaa Ravi
 
PPTX
El Gamal Cryptosystem
Adri Jovin
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPT
Pattern matching
shravs_188
 
PPT
DES (Data Encryption Standard) pressentation
sarhadisoftengg
 
PDF
Python exception handling
Mohammed Sikander
 
PPTX
Hash function
Salman Memon
 
PPTX
Introduction to matplotlib
Piyush rai
 
PPTX
Character set of c
Chandrapriya Rediex
 
Introduction to Python
amiable_indian
 
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
 
Binary Search Tree and Heap in Prolog
Benjamin Guillet
 
List in Python
Sharath Ankrajegowda
 
arrays in c
vidhi mehta
 
Python basics
RANAALIMAJEEDRAJPUT
 
php basics
Anmol Paul
 
CNS - Unit - 2 - Stream Ciphers and Block Ciphers
Gyanmanjari Institute Of Technology
 
PythonOOP
Veera Pendyala
 
Lecture 02 lexical analysis
Iffat Anjum
 
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
Structure of the compiler
Sudhaa Ravi
 
El Gamal Cryptosystem
Adri Jovin
 
Pattern matching
shravs_188
 
DES (Data Encryption Standard) pressentation
sarhadisoftengg
 
Python exception handling
Mohammed Sikander
 
Hash function
Salman Memon
 
Introduction to matplotlib
Piyush rai
 
Character set of c
Chandrapriya Rediex
 

Viewers also liked (20)

PDF
PHP Unit 4 arrays
Kumar
 
PDF
Php array
Nikul Shah
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PPTX
PHP array 1
Mudasir Syed
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPT
03 Php Array String Functions
Geshan Manandhar
 
PDF
PHP Basic & Arrays
M.Zalmai Rahmani
 
PPT
Synapseindia reviews on array php
saritasingh19866
 
PPTX
Array in php
Ashok Kumar
 
ODP
My self learn -Php
laavanyaD2009
 
PPTX
Arrays PHP 03
mohamedsaad24
 
PPT
Php Using Arrays
mussawir20
 
PPT
Oops in PHP By Nyros Developer
Nyros Technologies
 
PPTX
Array in php
ilakkiya
 
PDF
Web app development_php_06
Hassen Poreya
 
PPTX
PHP Array very Easy Demo
Salman Memon
 
PPTX
Conheça mais o SlideShare
Rafael Pinheiro
 
PPT
Aula 5 encapsulamento, associação, polimorfismo, interfaces
Rafael Pinheiro
 
PPTX
How to Create an Array & types in PHP
Ajit Sinha
 
PHP Unit 4 arrays
Kumar
 
Php array
Nikul Shah
 
Arrays in PHP
Vineet Kumar Saini
 
PHP array 1
Mudasir Syed
 
PHP Functions & Arrays
Henry Osborne
 
03 Php Array String Functions
Geshan Manandhar
 
PHP Basic & Arrays
M.Zalmai Rahmani
 
Synapseindia reviews on array php
saritasingh19866
 
Array in php
Ashok Kumar
 
My self learn -Php
laavanyaD2009
 
Arrays PHP 03
mohamedsaad24
 
Php Using Arrays
mussawir20
 
Oops in PHP By Nyros Developer
Nyros Technologies
 
Array in php
ilakkiya
 
Web app development_php_06
Hassen Poreya
 
PHP Array very Easy Demo
Salman Memon
 
Conheça mais o SlideShare
Rafael Pinheiro
 
Aula 5 encapsulamento, associação, polimorfismo, interfaces
Rafael Pinheiro
 
How to Create an Array & types in PHP
Ajit Sinha
 
Ad

Similar to Php array (20)

PDF
Perl 6 in Context
lichtkind
 
PPT
Basic PHP
Todd Barber
 
ODP
Introduction to Perl - Day 1
Dave Cross
 
PDF
Scripting3
Nao Dara
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PPT
Php Basic
Md. Sirajus Salayhin
 
PPTX
Marcs (bio)perl course
BITS
 
PDF
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
ODP
Introduction to Perl - Day 2
Dave Cross
 
ODP
Introduction to Perl
Dave Cross
 
PPTX
Introduction to PHP Lecture 1
Ajay Khatri
 
ODP
Writing Maintainable Perl
tinypigdotcom
 
ODP
Beginning Perl
Dave Cross
 
PPTX
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
PPTX
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
ODP
Perl Introduction
Marcos Rebelo
 
ODP
Intermediate Perl
Dave Cross
 
PPTX
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
PPT
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Perl 6 in Context
lichtkind
 
Basic PHP
Todd Barber
 
Introduction to Perl - Day 1
Dave Cross
 
Scripting3
Nao Dara
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Marcs (bio)perl course
BITS
 
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
Introduction to Perl - Day 2
Dave Cross
 
Introduction to Perl
Dave Cross
 
Introduction to PHP Lecture 1
Ajay Khatri
 
Writing Maintainable Perl
tinypigdotcom
 
Beginning Perl
Dave Cross
 
UNIT IV (4).pptx
DrDhivyaaCRAssistant
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Dhivyaa C.R
 
Perl Introduction
Marcos Rebelo
 
Intermediate Perl
Dave Cross
 
PHP PPT.pptxPHP PPT.pptxPHP PPT.pptxPHP n
ArtiRaju1
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
Roy Zimmer
 
Ad

Recently uploaded (20)

PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 

Php array

  • 1. PHP Arrays An indexed array is similar to one provided by a conventional programming language. An element of an associative array can be accessed by a keyword. An associative array is like a dictionary or map . An array element can be of any type. An array can be heterogeneous with its element types and structure. Many functions manipulating an array are provided. PHP Array
  • 2. Indexed Array PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); echo &quot;$animals[0]\n&quot;; echo &quot;$animals[2]\n&quot;; echo &quot;$animals\n&quot;; dog fish Array
  • 3. Updating and Adding Elements PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); echo &quot;$animals[1]\n&quot;; $animals[1] = &quot; tiger &quot; ; echo &quot;$animals[1]\n&quot;; $animals[] = &quot;beaver&quot;; echo &quot;$animals[3]\n&quot;; cat tiger beaver
  • 4. Associative Array PHP Array $animals = array( &quot;dog“ => 15,&quot;cat“ = >8, &quot;fish“ => 2); echo &quot;$animals[cat]\n&quot;; $animals[&quot;bat&quot;] = 100; echo &quot;$animals[bat]\n&quot;; 8 100
  • 5. Listing array element : for PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); for ($i = 0; $i < count($animals); $i++) { echo $i . &quot;-th animal is a $animals[$i].\n&quot;; } 0-th animal is a dog. 1-th animal is a cat. 2-th animal is a fish.
  • 6. Listing Array Elements: foreach PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); foreach ($animals as $animal) echo &quot;$animal\n&quot;; } dog cat fish
  • 7. while and each PHP Array $animals = array( &quot;dog“ => 15,&quot;cat“ => 8, &quot;fish“ => 2); while ($item = each($animals)) print &quot;weight of &quot; . $item[&quot;key&quot;] . &quot; is &quot; . $item[&quot;value&quot;] . “.\n&quot;; weight of dog is 15. weight of cat is 8. weight of fish is 2.
  • 8. each and list PHP Array $animals = array( &quot;dog“ => 15, &quot;cat“ => 8, &quot;fish“ => 2); while (list($key, $value) = each($animals)) print &quot;weight of $key is $value.\n&quot;; weight of dog is 15. weight of cat is 8. weight of fish is 2.
  • 9. Multi-Dimensional Heterogeneous Array PHP Array $books = array( array(&quot;title“ => “A&quot;, &quot;author“ => “X&quot;), array(&quot;title“ => “B&quot;, “ author“ => “Y&quot;, “ price“ => 25) ); print_r($books); Array ( [0] => Array ( [title] => A [author] => X ) [1] => Array ( [title] => B [author] => Y [price] => 25 ) )
  • 10. Nested Loops PHP Array for ($i=0; $i < count($books); $i++) { print &quot;$i-th book is:&quot;; while ( list($key, $value) = each($books[$i]) ) print “ $key: $value&quot;; print &quot;\n&quot;; } 0-th book is: title: A author: X 1-th book is: title: B author: Y price: 25
  • 11. String as an Array PHP Array $myString = &quot;My chars&quot;; echo &quot;$myString\n&quot;; echo &quot;$myString[1]\n&quot;; My chars y
  • 12. Array functions count(), sizeof() in_array() array_slice () array_pad() array_reverse() list( ) Sorting Functions sort(), rsort() asort(), arsort() ksort(), krsort() PHP Array
  • 13. count(array1) and sizeof(array1) PHP Array Returns the size of array array1 . $animals = array ('dog', 'cat', 'fish'); echo count($animals); echo sizeof($animals); 3 3
  • 14. array array_pad(array1, length, value) PHP Array Pad array to length length with value . $scores = array(1, 2); $padded = array_pad($scores, 5, 10); print_r($padded); Array ( [0] => 1 [1] => 2 [2] => 10 [3] => 10 [4] => 10 )
  • 15. array array_reverse(array1) PHP Array Return an array with elements in reverse order. Array ( [0] => fish [1] => cat [2] => dog ) $animals = array('dog', 'cat', 'fish'); $reversed array_reverse($animals); print_r($reversed);
  • 16. array array_slice (array1, offset, length) PHP Array Extract a slice of length from array1 starting at offset . $array1 = array(1, 2, 3, 4, 5, 6); $subarray = array_slice(array1, 3, 2); print_r($subarray); Array ( [0] => 4 [1] => 5 )
  • 17. boolean in_array(value, array1) PHP Array Check if value exists in array1 . $animals = array('dog', 'cat', 'fish'); echo in_array('cat', $animals); echo in_array(‘monkey', $animals); 1 (true) (false)
  • 18. void list(var1, var2, ...) PHP Array The elements of an array are copied into the list of variables var1, var2, . . . $animals = array('dog', 'cat', 'fish'); list($a, $b, $c) = $animals; echo &quot;$a, $b, $c&quot;; dog, cat, fish
  • 19. sort(array) and rsort(array) PHP Array Sort the elements in an array in increasing or decreasing order. $animals = array('dog', 'cat', fish'); sort($animals); print_r($animals); Array ( [0] => cat [1] => dog [2] => fish )
  • 20. asort(array), arsort(array) PHP Array Sort an array maintaining index association. $animals = array('dog', 'cat', 'fish'); asort($animals); print_r($animals); Array ( [1] => cat [0] => dog [2] => fish )
  • 21. ksort(array), krsort(array) PHP Array Sort array by keys. $animals = array('dog' => 15, 'cat' => 8, 'fish' => 2); ksort($animals); print_r($animals); Array ( [cat] => 8 [dog] => 15 [fish] => 12 )