Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed.

Similar presentations


Presentation on theme: "1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed."— Presentation transcript:

1 1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed Arrays Associative Arrays Associative Arrays Array Functions Array Functions

2 2 PHP Array PHP Arrays An indexed array is similar to one provided by a conventional programming language. 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 element of an associative array can be accessed by a keyword. An associative array is like a dictionary with key and value. An associative array is like a dictionary with key and value. An array element can be of any type. An array element can be of any type. An array can be heterogeneous with its element types and structure. An array can be heterogeneous with its element types and structure. Many functions manipulating an array are provided. Many functions manipulating an array are provided.

3 3 PHP Array Indexed Array $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); echo "$animals[0]\n"; echo "$animals[2]\n"; echo "$animals\n"; dog fish Array

4 4 PHP Array Updating and Adding Elements $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); echo "$animals[1]\n"; $animals[1] = "tiger"; echo "$animals[1]\n"; $animals[] = "beaver"; echo "$animals[3]\n"; cat tiger beaver

5 5 PHP Array Associative Array $animals = array( "dog“ => 15,"cat“ => 8, "fish“ => 2); "dog“ => 15,"cat“ => 8, "fish“ => 2); echo "$animals[cat]\n"; echo "$animals[cat]\n"; $animals["bat"] = 100; $animals["bat"] = 100; echo "$animals[bat]\n"; echo "$animals[bat]\n"; 8 100

6 6 PHP Array Listing array element : for $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); for ($i = 0; $i < count($animals); $i++) { echo $i. "-th animal is a $animals[$i].\n"; echo $i. "-th animal is a $animals[$i].\n";} 0-th animal is a dog. 1-th animal is a cat. 2-th animal is a fish.

7 7 PHP Array Listing Array Elements: foreach $animals = $animals = array("dog", "cat", "fish"); array("dog", "cat", "fish"); foreach ($animals as $animal) foreach ($animals as $animal) echo "$animal\n"; echo "$animal\n"; } dog cat fish

8 8 PHP Array while and each $animals = array( "dog“ => 15,"cat“ => 8, "fish“ => 2); "dog“ => 15,"cat“ => 8, "fish“ => 2); while ($item = each($animals)) print "weight of ". print "weight of ". $item["key"]. " is ". $item["key"]. " is ". $item["value"]. “.\n"; $item["value"]. “.\n"; weight of dog is 15. weight of cat is 8. weight of fish is 2.

9 9 PHP Array each and list $animals = array( "dog“ => 15, "cat“ => 8, "fish“ = >2); "dog“ => 15, "cat“ => 8, "fish“ = >2); while (list($key, $value)=each($animals)) print "weight of $key is $value.\n"; print "weight of $key is $value.\n"; weight of dog is 15. weight of cat is 8. weight of fish is 2.

10 10 PHP Array Multi-Dimensional Heterogeneous Array $books = array( array("title“ => “A", "author“ => “X"), "author“ => “X"), array("title“ => “B", “author“ => “Y", “author“ => “Y", “price“ => 25) “price“ => 25));print_r($books); Array ( [0] => Array ( [title] => A [author] => X ) [1] => Array ( [title] => B [author => Y [price] => 25 )

11 11 PHP Array Nested Loops for ($i=0; $i < count($books); $i++) { print "$i-th book is:"; print "$i-th book is:"; while ( list($key, $value) = while ( list($key, $value) = each($books[$i]) ) each($books[$i]) ) print “ $key: $value"; print “ $key: $value"; print "\n"; print "\n";} 0-th book is: title: A author: X 1-th book is: title: B author: Y price: 25

12 12 PHP Array String as an Array $myString = "My chars"; echo "$myString\n"; echo "$myString[1]\n"; My chars y

13 13 PHP Array Array functions count(), sizeof() count(), sizeof() in_array() in_array() array_slice() array_slice() array_reverse() array_reverse() list( ) list( ) Sorting Functions Sorting Functions sort(), rsort() sort(), rsort() asort(), arsort() asort(), arsort() ksort(), krsort() ksort(), krsort()

14 14 PHP Array count(array1) and sizeof(array1) Returns the size of array array1. $animals = array ('dog', 'cat', 'fish'); echo count($animals); echo sizeof($animals); 3333

15 15 PHP Array array array_reverse(array1) Return an array with elements in reverse order. Array ( [0] = fish [1] = cat [2] = dog ) $animals = array('dog', 'cat', 'fish'); array('dog', 'cat', 'fish'); $reversed = array_reverse($animals); print_r($reversed);

16 16 PHP Array array array_slice (array1, offset, length) Extract a slice of length from array1 starting at offset. $array1 = array(1, 2, 3, 4, 5, 6); array(1, 2, 3, 4, 5, 6); $subarray = array_slice($array1, 3, 2); array_slice($array1, 3, 2);print_r($subarray); Array ( [0] = 4 [1] = 5 )

17 17 PHP Array boolean in_array(value, array1) 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 18 PHP Array void list(var1, var2,...) 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 "$a, $b, $c"; dog, cat, fish

19 19 PHP Array sort(array) and rsort(array) Sort the elements in an array in increasing or decreasing order. $animals = array('dog', 'cat', 'fish'); array('dog', 'cat', 'fish');sort($animals);print_r($animals); Array ( [0] = cat [1] = dog [2] = fish )

20 20 PHP Array asort(array), arsort(array) Sort array by values and maintain index association $animals = array('dog', 'cat', 'fish'); array('dog', 'cat', 'fish');asort($animals);print_r($animals); Array ( [1] = cat [0] = dog [2] = fish )

21 21 PHP Array ksort(array), krsort(array) Sort array by keys. $animals = array('dog' => 15, array('dog' => 15, 'cat' => 8, 'fish' => 2); ksort($animals);print_r($animals); Array ( [cat] => 8 [dog] => 15 [fish] => 12 )


Download ppt "1 PHP Intro PHP Arrays After this lecture, you should be able to: Create and manipulate PHP Arrays: Create and manipulate PHP Arrays: Indexed Arrays Indexed."

Similar presentations


Ads by Google