Presentation is loading. Please wait.

Presentation is loading. Please wait.

>> PHP: Arrays.

Similar presentations


Presentation on theme: ">> PHP: Arrays."— Presentation transcript:

1 >> PHP: Arrays

2 Arrays Arrays Syntax Holds a list of same type elements
$varName = array(‘a‘, ‘b‘, ‘c‘); Accessing Array elements varName[key]; Length of an Array count(varName); Separate using coma’s Keys Value a 1 b 2 c Keyword

3 Associative Arrays: Arrays with custom keys Syntax
$varName = array(‘key1‘=>’a’, ‘key2‘=>’b’); Accessing Array elements varName[‘key1’]; Key names (Strings or text) Keyword Keys Value key1 a key2 b

4 Types of Arrays in PHP Arrays Arrays Associative Arrays
Declaration-time initialization $arr = array(“a”, “b”, “c”); Initialization after declaration $arr = array(); $arr[0] = “a”; Indexes are assigned automatically [0], [1], [2] Declaration-time initialization $arr = array(101 => “a”, 102 => “b”, 103 => “c”); Initialization after declaration $arr = array(); $arr[101] = “a”; Indexes are assigned manually [101], [102], [103] Array-name Accessing Elements Working Variable (like the i in your for loop) foreach($arr as $a) { echo $a; }

5 Multi-dimensional Array
A multidimensional array is an array containing one or more arrays. In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on. $cars = array   (   array("Volvo",100,96),   array("BMW",60,59),   array("Toyota",110,100)   ); Volvo 100 96 BMW 60 59 Toyota 110 Associative Multi-Dimensional Arrays $cars = array(); $cars[101] =array(“name”=>"Volvo",”val”=>100,”val2”=>96); $cars[102] = array(“name”=>"BMW",”val”=>60,”val2”=>59); $cars[103] = array(“name”=>"Toyota",”val”=>110,”val2”=>100);

6 Mini Project Our Old Files Go Online index.php header.php footer.php
contact.php Go Online Download the stuff.php file Copy in the folder (D:\xampp\htdocs\Buy4mMe\) Now open the project in Aptana/NetBeans

7 Setting up the Stuffs Array
Open the stuff.php page Create the following multi-dimensional array named stuff First dimension is the product numbers (101, 102) Second Dimension includes the attributes (name, img, price) TRY NOW 101 102 Amazon Echo AR Drone img/stuff/stuff-101.jpg img/stuff/stuff-102.jpg 600 2500 “name” => “img” => “price” =>

8 Importing other elements
Go Online Download & Open the file products-array.txt Copy the array declaration after your initializations TRY NOW

9 Displaying the List TRY NOW <li> <a href=“#”>
Inside the div tag with class=“wrapper” under the h1 heading element, create an unordered list. Set the class of the unordered list to “products” Now we want to list the items one by one TRY NOW The location of the image The name of the product <li> <a href=“#”> <img src=“ ” alt=“ “/> <p>View Details</p> </a> </li>

10 Web-Based Systems - Misbhauddin
Next on PHP Database Handling MySQL phpMyAdmin Web-Based Systems - Misbhauddin


Download ppt ">> PHP: Arrays."

Similar presentations


Ads by Google