Uploaded on

 

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,574
On Slideshare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
64
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Arrays in PHP By Komal Arora
  • 2. What is Array? An array is a data structure that stores one or more values in a single value. An array in PHP is actually an ordered map. It associates values to keys....
  • 3. Types of Arrays Indexed Vs Associative Arrays The keys of an indexed array are integers, beginning at 0. Indexed arrays are used when you identify things by their position. $b = array('a', 'b', 'c');
  • 4. Associative arrays have strings as keys and behave more like two-column tables. The first column is the key, which is used to access the value. $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 );
  • 5. Syntax An array can be created by array() . To construct an empty array, pass no arguments to array( ): $addresses = array( ); It takes as parameters any number of comma-separated key => value pairs. Example: $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; echo $arr[12]; OR print_r($arr);
  • 6. Accessing Array elements $b = array('a', 'b', 'c'); $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; RESULT=array(0 => 'a', 1 => 'b', 2 => 'c')‏
  • 7. $a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name' => 'apple', 4 // key will be 0 ); // . . .is completely equivalent with this: $a = array(); $a['color'] = 'red'; $a['taste'] = 'sweet'; $a['shape'] = 'round'; $a['name'] = 'apple'; $a[] = 4;
  • 8. Example <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo &quot;Do you like $color?n&quot;; } ?>
  • 9. Getting the Size of an Array The count( ) and sizeof( ) functions are identical in use and effect. They return the number of elements in the array.
  • 10. Checking Whether an Element Exists isset( ) function is used, which returns true if the element exists and is not NULL:
  • 11. Traversing Arrays Using Foreach:- $addresses = array('spam@cyberpromo.net', 'abuse@example.com'); foreach ($addresses as $value) { echo &quot;Processing $valuen&quot;; } Using a for Loop: $addresses = array('spam@cyberpromo.net', 'abuse@example.com'); for($i = 0; $i < count($array); $i++) { $value = $addresses[$i]; echo &quot;$valuen&quot;; }
  • 12. Creating Variables from an Array The extract( ) function automatically creates local variables from an array. $shape = &quot;round&quot;; $array = array(&quot;cover&quot; => &quot;bird&quot;, &quot;shape&quot; => &quot;rectangular&quot;); extract($array, EXTR_PREFIX_SAME, &quot;book&quot;); echo &quot;Cover: $book_cover, Book Shape: $book_shape, Shape: $shape&quot;; Cover: bird, Book Shape: rectangular, Shape: round
  • 13. Array functions Lets discuss this on PHP.net
  • 14.