Php array

2,070 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,070
On SlideShare
0
From Embeds
0
Number of Embeds
45
Actions
Shares
0
Downloads
84
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php array

  1. 1. PHP Arrays <ul><li>An indexed array is similar to one provided by a conventional programming language. </li></ul><ul><li>An element of an associative array can be accessed by a keyword. </li></ul><ul><li>An associative array is like a dictionary or map . </li></ul><ul><li>An array element can be of any type. </li></ul><ul><li>An array can be heterogeneous with its element types and structure. </li></ul><ul><li>Many functions manipulating an array are provided. </li></ul>PHP Array
  2. 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;$animalsn&quot;; dog fish Array
  3. 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. 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. 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. 6. Listing Array Elements: foreach PHP Array $animals = array(&quot;dog&quot;, &quot;cat&quot;, &quot;fish&quot;); foreach ($animals as $animal) echo &quot;$animaln&quot;; } dog cat fish
  7. 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. 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. 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. 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. 11. String as an Array PHP Array $myString = &quot;My chars&quot;; echo &quot;$myStringn&quot;; echo &quot;$myString[1]n&quot;; My chars y
  12. 12. Array functions <ul><li>count(), sizeof() </li></ul><ul><li>in_array() </li></ul><ul><li>array_slice () </li></ul><ul><li>array_pad() </li></ul><ul><li>array_reverse() </li></ul><ul><li>list( ) </li></ul><ul><li>Sorting Functions </li></ul><ul><ul><li>sort(), rsort() </li></ul></ul><ul><ul><li>asort(), arsort() </li></ul></ul><ul><ul><li>ksort(), krsort() </li></ul></ul>PHP Array
  13. 13. count(array1) and sizeof(array1) PHP Array Returns the size of array array1 . <ul><ul><li>$animals = array ('dog', 'cat', 'fish'); </li></ul></ul><ul><ul><li>echo count($animals); </li></ul></ul><ul><ul><li>echo sizeof($animals); </li></ul></ul>3 3
  14. 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. 15. array array_reverse(array1) PHP Array Return an array with elements in reverse order. Array ( [0] => fish [1] => cat [2] => dog ) <ul><ul><li>$animals = </li></ul></ul><ul><ul><li>array('dog', 'cat', 'fish'); </li></ul></ul><ul><ul><li>$reversed array_reverse($animals); </li></ul></ul><ul><ul><li>print_r($reversed); </li></ul></ul>
  16. 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. 17. boolean in_array(value, array1) PHP Array Check if value exists in array1 . <ul><ul><li>$animals = array('dog', 'cat', 'fish'); </li></ul></ul><ul><ul><li>echo in_array('cat', $animals); </li></ul></ul><ul><ul><li>echo in_array(‘monkey', $animals); </li></ul></ul>1 (true) (false)
  18. 18. void list(var1, var2, ...) PHP Array The elements of an array are copied into the list of variables var1, var2, . . . <ul><ul><li>$animals = array('dog', 'cat', 'fish'); </li></ul></ul><ul><ul><li>list($a, $b, $c) = $animals; </li></ul></ul><ul><ul><li>echo &quot;$a, $b, $c&quot;; </li></ul></ul>dog, cat, fish
  19. 19. sort(array) and rsort(array) PHP Array Sort the elements in an array in increasing or decreasing order. <ul><ul><li>$animals = </li></ul></ul><ul><ul><li>array('dog', 'cat', fish'); </li></ul></ul><ul><ul><li>sort($animals); </li></ul></ul><ul><ul><li>print_r($animals); </li></ul></ul>Array ( [0] => cat [1] => dog [2] => fish )
  20. 20. asort(array), arsort(array) PHP Array Sort an array maintaining index association. <ul><ul><li>$animals = </li></ul></ul><ul><ul><li>array('dog', 'cat', 'fish'); </li></ul></ul><ul><ul><li>asort($animals); </li></ul></ul><ul><ul><li>print_r($animals); </li></ul></ul>Array ( [1] => cat [0] => dog [2] => fish )
  21. 21. ksort(array), krsort(array) PHP Array Sort array by keys. <ul><ul><li>$animals = </li></ul></ul><ul><ul><li>array('dog' => 15, </li></ul></ul><ul><ul><li>'cat' => 8, </li></ul></ul><ul><ul><li>'fish' => 2); </li></ul></ul><ul><ul><li>ksort($animals); </li></ul></ul><ul><ul><li>print_r($animals); </li></ul></ul>Array ( [cat] => 8 [dog] => 15 [fish] => 12 )

×