Your SlideShare is downloading. ×
  • Like
Arrays in PHP
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply


Published 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


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 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('', ''); foreach ($addresses as $value) { echo &quot;Processing $valuen&quot;; } Using a for Loop: $addresses = array('', ''); 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
  • 14.