• Like
  • Save
Arrays in PHP
Upcoming SlideShare
Loading in...5
×
 

Arrays in PHP

on

  • 1,815 views

 

Statistics

Views

Total Views
1,815
Views on SlideShare
1,714
Embed Views
101

Actions

Likes
0
Downloads
53
Comments
0

5 Embeds 101

http://prosjekt.hia.no 57
http://www.ustudy.in 41
http://ustudy.in 1
http://ustudy.in. 1
http://prosjekt.uia.no 1

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Arrays in PHP Arrays in PHP Presentation Transcript

    • 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 an ordered map. It associates values to keys....
    • 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');
    • 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 );
    • 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);
    • Accessing Array elements $b = array('a', 'b', 'c'); $b = array(); $b[] = 'a'; $b[] = 'b'; $b[] = 'c'; RESULT=array(0 => 'a', 1 => 'b', 2 => 'c')‏
    • $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;
    • Example <?php $colors = array('red', 'blue', 'green', 'yellow'); foreach ($colors as $color) { echo &quot;Do you like $color?n&quot;; } ?>
    • 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.
    • 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 $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;; }
    • 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
    • Array functions Lets discuss this on PHP.net
    •