Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Design 2 Using PHP PHP Arrays.

Similar presentations


Presentation on theme: "Web Design 2 Using PHP PHP Arrays."— Presentation transcript:

1 Web Design 2 Using PHP PHP Arrays

2 Array An array is a special variable, which can hold more than one value at a time. In PHP, the array() is used to create an array: array(); In PHP we have 3 types of arrays: Indexed arrays - Arrays with a numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays

3 Integer Indices There <?php $stuff = array("Hi", "There");
echo $stuff[1], "\n"; ?> There

4 Key / Value Web Design 2 <?php $stuff = array("name" => "Chuck",
"course" => “Web Design 2 "); echo $stuff["course"], "\n"; ?> Web Design 2

5 Dumping an Array The function print_r() dumps out PHP data - it is used mostly for debugging <?php $stuff = array("name" => "Chuck", "course" => “Web Design 2 "); echo $stuff["course"], "\n"; ?> Array( [name] => Chuck [course] => Web Design 2 )

6 Building up an Array You can allocate a new item in the array and add a value at the same time using empty square braces [] on the right hand side of an assignment statement $va = array(); $va[] = "Hello"; $va[] = "World"; print_r($va); Array( [0] => Hello [1] => World )

7 Building up an Array You can also add new items in an array using a key as well $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; print_r($za); Array( [name] => Chuck [course] => PHPIntro )

8 Looping Through an Array
<?php $stuff = array("name" => "Chuck", "course" => “Web Design 2"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=name Val=Chuck Key=course Val= Web Design 2

9 Looping Through an Array
<?php $colors= array(“red", “yellow", “black"); $a_length = count($colors); for($x = 0; $x <  $a_length; $x++) {      echo $colors[$x];      echo "<br>"; } ?> red yellow black

10 Arrays of Arrays $majors = array ( array("archtecural eng","computer eng","telecom eng"), array("CIS","CS","SE"), array(" Accounting ","Marketing","Finance"), ); echo " In Engineering: ".$majors[0][0].", ".$majors[0][1].", and, ".$majors[0][2].".<br>"; echo " In IT : ".$majors[1][0].", ".$majors[1][1].", and ".$majors[1][2].".<br>"; echo " In administration sciences: ".$majors[2][0].", ".$majors[2][1].", and ".$majors[2][2].".<br>"; The elements of an array can be many things other than a string or integer. You can even have objects or other arrays. In Engineering: archtecural eng, computer eng, and, telecom eng. In IT : CIS, CS, and SE. In administration sciences: Accounting, Marketing, and Finance.

11 $course= array( "IT" => array( "Java" => "CIS", "Sql" => "CS"), "eng" => array( "autocad"=>"Architectural Eng", "Alg" => "Computer Eng") ) ; echo $course["IT"]["Java"]; Arrays of Arrays The elements of an array can be many things other than a string or integer. You can even have objects or other arrays. CIS

12 Array Functions count($ar) - How many elements in an array
is_array($ar) - Returns TRUE if a variable is an array isset($ar['key']) - Returns TRUE if key is set in the array sort($ar) - Sorts the array values (loses key) ksort($ar) - Sorts the array by key asort($ar) - Sorts array by value, keeping key association shuffle($ar) - Shuffles the array into random order

13 Count: 2 $za Is an array name is set addr is not set $za = array();
$za["name"] = "Chuck"; $za["course"] = “WD2"; print "Count: " . count($za) . "\n"; if ( is_array($za) ) { echo '$za Is an array' . "\n"; } else { echo '$za Is not an array' . "\n"; echo isset($za['name']) ? "name is set\n" : "name is not set\n"; echo isset($za['addr']) ? "addr is set\n" : "addr is not set\n"; Count: 2 $za Is an array name is set addr is not set

14 Array( [name] => Chuck [course] => WD2 [topic] => PHP ) [0] => Chuck [1] => PHP [2] => WD2 $za = array(); $za["name"] = "Chuck"; $za["course"] = “WD2"; $za["topic"] = "PHP"; print_r($za); sort($za);

15 Array( [name] => Chuck [course] => WD2 [topic] => PHP ) $za = array(); $za["name"] = "Chuck"; $za["course"] = “WD2"; $za["topic"] = "PHP"; print_r($za); ksort($za); asort($za);

16 PHP array Functions Visit the following website: Then explore the PHP arrays functions as shown in the next slide.

17 Array Functions: array_chunk — Split an array into chunks array_combine — Creates an array by using one array for keys and another for its values array_column — Return the values from a single column in the input array array_count_values — Counts all the values of an array array_diff — Computes the difference of arrays array_intersect — Computes the intersection of arrays array_merge — Merge one or more arrays array_pop — Pop the element off the end of array array_push — Push one or more elements onto the end of array array_rand — Pick one or more random entries out of an array array_shift — Shift an element off the beginning of array array_sum — Calculate the sum of values in an array array_search — Searches the array for a given value and returns the corresponding key if successful array_reverse — Return an array with elements in reverse order in_array — Checks if a value exists in an array what is the difference between: empty() ,is_null() ,isset() unset()

18 Summary PHP arrays are a very powerful associative array as they can be indexed by integers like a list, or use keys to look values up like a hash map or dictionary PHP arrays maintain order and there are many options for sorting


Download ppt "Web Design 2 Using PHP PHP Arrays."

Similar presentations


Ads by Google