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

PPTX
Introduction to Basics of Python
PPTX
Java package
PPT
Php Using Arrays
PPS
Introduction to class in java
PPT
Object-oriented concepts
PPTX
Inheritance In Java
PPTX
Packages in java
PPT
Class 5 - PHP Strings
Introduction to Basics of Python
Java package
Php Using Arrays
Introduction to class in java
Object-oriented concepts
Inheritance In Java
Packages in java
Class 5 - PHP Strings

What's hot (20)

PPTX
Operators in java
PDF
CS3391 -OOP -UNIT – IV NOTES FINAL.pdf
PPTX
computer viruses power point presentation
PPTX
OOPS In JAVA.pptx
PDF
What Are Python Modules? Edureka
PPTX
Android activity lifecycle
PPT
PHP POWERPOINT SLIDES
PPTX
OOP concepts -in-Python programming language
PPTX
Programming in c Arrays
PPTX
Tokens in C++
PPT
Web Technology - PHP Arrays
PPTX
Python Functions
PDF
Arrays In Python | Python Array Operations | Edureka
PPTX
Java packages
PPSX
Modules and packages in python
PPT
PHP variables
PDF
Structures and Pointers
PDF
PPT
Java Streams
PPTX
Java awt (abstract window toolkit)
Operators in java
CS3391 -OOP -UNIT – IV NOTES FINAL.pdf
computer viruses power point presentation
OOPS In JAVA.pptx
What Are Python Modules? Edureka
Android activity lifecycle
PHP POWERPOINT SLIDES
OOP concepts -in-Python programming language
Programming in c Arrays
Tokens in C++
Web Technology - PHP Arrays
Python Functions
Arrays In Python | Python Array Operations | Edureka
Java packages
Modules and packages in python
PHP variables
Structures and Pointers
Java Streams
Java awt (abstract window toolkit)
Ad

Viewers also liked (20)

PPT
PDF
PHP Unit 4 arrays
PDF
Php array
PDF
Arrays in PHP
PPTX
PHP array 1
PPTX
PHP Functions & Arrays
PPT
03 Php Array String Functions
PDF
PHP Basic & Arrays
PPT
Synapseindia reviews on array php
PPTX
Array in php
ODP
My self learn -Php
PPTX
Arrays PHP 03
PPT
Oops in PHP By Nyros Developer
PPTX
Array in php
PDF
Web app development_php_06
PPTX
PHP Array very Easy Demo
PPTX
Conheça mais o SlideShare
PPT
Aula 5 encapsulamento, associação, polimorfismo, interfaces
PPTX
How to Create an Array & types in PHP
PHP Unit 4 arrays
Php array
Arrays in PHP
PHP array 1
PHP Functions & Arrays
03 Php Array String Functions
PHP Basic & Arrays
Synapseindia reviews on array php
Array in php
My self learn -Php
Arrays PHP 03
Oops in PHP By Nyros Developer
Array in php
Web app development_php_06
PHP Array very Easy Demo
Conheça mais o SlideShare
Aula 5 encapsulamento, associação, polimorfismo, interfaces
How to Create an Array & types in PHP
Ad

Similar to Php array (20)

PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
PPT
Class 4 - PHP Arrays
PPT
PHP - Introduction to PHP Arrays
PPTX
Chapter 2 wbp.pptx
PPT
PHP-04-Arrays.ppt
PPTX
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PDF
4.1 PHP Arrays
PPTX
Arrays in PHP
PPTX
Array
PPT
PHP array 2
PPTX
Unit 2-Arrays.pptx
PPT
Arrays in php
PDF
Php tips-and-tricks4128
PDF
PHP tips and tricks
PPTX
Web Application Development using PHP Chapter 4
PPTX
Arrays syntax and it's functions in php.pptx
PDF
PHP and MySQL Tips and tricks, DC 2007
PDF
Array String - Web Programming
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Class 4 - PHP Arrays
PHP - Introduction to PHP Arrays
Chapter 2 wbp.pptx
PHP-04-Arrays.ppt
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
4.1 PHP Arrays
Arrays in PHP
Array
PHP array 2
Unit 2-Arrays.pptx
Arrays in php
Php tips-and-tricks4128
PHP tips and tricks
Web Application Development using PHP Chapter 4
Arrays syntax and it's functions in php.pptx
PHP and MySQL Tips and tricks, DC 2007
Array String - Web Programming

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
cuic standard and advanced reporting.pdf
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPT
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Review of recent advances in non-invasive hemoglobin estimation
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
Teaching material agriculture food technology

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 )