Arrays in PHP By  Komal Arora
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...
Types of Arrays Indexed Vs Associative Arrays The keys of an  indexed array  are integers, beginning at 0. Indexed arrays ...
Associative arrays  have strings as keys and behave more like two-column tables. The first column is the key, which is use...
Syntax An array can be created by  array()  . To construct an empty array, pass no arguments to array( ): $addresses = arr...
Accessing Array elements $b = array('a', 'b', 'c'); $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; RESULT=array(0 => 'a...
$a = array( 'color' => 'red', 'taste' => 'sweet', 'shape' => 'round', 'name'  => 'apple', 4  // key will be 0 ); // . . .i...
Example <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo &quot;Do you like $col...
Getting the Size of an Array The  count( )  and  sizeof( )  functions are identical in use and effect. They return the num...
Checking Whether an Element Exists isset( )  function is used, which returns true if the element exists and is not NULL:
Traversing Arrays Using Foreach:- $addresses = array('spam@cyberpromo.net', 'abuse@example.com'); foreach ($addresses as $...
Creating Variables from an Array The  extract( )  function automatically creates local variables from an array. $shape = &...
Array functions Lets discuss this on PHP.net
 
Upcoming SlideShare
Loading in...5
×

Arrays in PHP

1,703

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
1,703
On Slideshare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
71
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Arrays in PHP

  1. 1. Arrays in PHP By Komal Arora
  2. 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. 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. 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. 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(&quot;foo&quot; => &quot;bar&quot;, 12 => true); echo $arr[&quot;foo&quot;]; echo $arr[12]; OR print_r($arr);
  6. 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. 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. 8. Example <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo &quot;Do you like $color?n&quot;; } ?>
  9. 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. 10. Checking Whether an Element Exists isset( ) function is used, which returns true if the element exists and is not NULL:
  11. 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. 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. 13. Array functions Lets discuss this on PHP.net
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×