1 php intro php arrays after this lecture, you should be able to: create and manipulate php arrays:...

21
1 PHP Intro PHP Intro PHP Arrays PHP Arrays After this lecture, you should After this lecture, you should be able to: be able to: Create and manipulate Create and manipulate PHP Arrays PHP Arrays : : Indexed Indexed Arrays Arrays Associative Associative Arrays Arrays Array Array Functions Functions

Upload: francis-heath

Post on 04-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

11PHP IntroPHP Intro

PHP ArraysPHP Arrays

After this lecture, you should be able After this lecture, you should be able to:to:

Create and manipulate Create and manipulate PHP ArraysPHP Arrays:: IndexedIndexed Arrays Arrays AssociativeAssociative Arrays Arrays Array Array FunctionsFunctions

22PHP ArrayPHP Array

PHP ArraysPHP Arrays

An An indexedindexed array is similar to one provided array is similar to one provided by a conventional programming language.by a conventional programming language.

An element of an An element of an associative associative array can be array can be accessed by a keyword. accessed by a keyword.

An associative array is like a An associative array is like a dictionarydictionary with with keykey and and valuevalue..

An array element can be of any type. An array element can be of any type. An array can be An array can be heterogeneousheterogeneous with its with its

element types and structure.element types and structure. Many Many functionsfunctions manipulating an array are manipulating an array are

provided.provided.

33PHP ArrayPHP Array

Indexed ArrayIndexed Array$animals = $animals =

array("dog", "cat", "fish");array("dog", "cat", "fish");

echo "$animals[0]\n";echo "$animals[0]\n";

echo "$animals[2]\n";echo "$animals[2]\n";

echo "$animals\n";echo "$animals\n";

dogfishArray

44PHP ArrayPHP Array

Updating and Adding ElementsUpdating and Adding Elements

$animals = $animals = array("dog", "cat", "fish");array("dog", "cat", "fish");

echo "$animals[1]\n"; echo "$animals[1]\n"; $animals[1] = "$animals[1] = "tigertiger"";; echo "$animals[1]\n";echo "$animals[1]\n";

$animals[] = "beaver";$animals[] = "beaver";echo "$animals[3]\n";echo "$animals[3]\n";

cat

tiger

beaver

55PHP ArrayPHP Array

Associative ArrayAssociative Array

$animals = array( $animals = array(

"dog“ => 15,"cat“ => 8, "fish“ => 2);"dog“ => 15,"cat“ => 8, "fish“ => 2);

echo "$animals[cat]\n";echo "$animals[cat]\n";

$animals["bat"] = 100;$animals["bat"] = 100;

echo "$animals[bat]\n"; echo "$animals[bat]\n";

8100

66PHP ArrayPHP Array

Listing array elementListing array element: : forfor

$animals =$animals =

array("dog", "cat", "fish");array("dog", "cat", "fish");

for ($i = 0; $i < count($animals); $i++) {for ($i = 0; $i < count($animals); $i++) {

echo $i . "-th animal is a $animals[$i].\n";echo $i . "-th animal is a $animals[$i].\n";

}}

0-th animal is a dog.1-th animal is a cat.2-th animal is a fish.

77PHP ArrayPHP Array

Listing Array Elements: Listing Array Elements: foreachforeach

$animals =$animals =

array("dog", "cat", "fish");array("dog", "cat", "fish");

foreach ($animals as $animal) foreach ($animals as $animal)

echo "$animal\n";echo "$animal\n";

}}

dogcatfish

88PHP ArrayPHP Array

whilewhile and and eacheach

$animals = array( $animals = array(

"dog“ => 15,"cat“ => 8, "fish“ => 2);"dog“ => 15,"cat“ => 8, "fish“ => 2);

while ($item = each($animals)) while ($item = each($animals))

print "weight of " . print "weight of " .

$item["key"] . " is " .$item["key"] . " is " .

$item["value"] . “.\n";$item["value"] . “.\n";

weight of dog is 15.weight of cat is 8.weight of fish is 2.

99PHP ArrayPHP Array

eacheach and and listlist$animals = array( $animals = array(

"dog“ => 15, "cat“ => 8, "fish“ = >2);"dog“ => 15, "cat“ => 8, "fish“ = >2);

while (list($key, $value)=each($animals))while (list($key, $value)=each($animals))

print "weight of $key is $value.\n";print "weight of $key is $value.\n";

weight of dog is 15.weight of cat is 8.weight of fish is 2.

1010PHP ArrayPHP Array

Multi-Dimensional Heterogeneous Multi-Dimensional Heterogeneous ArrayArray

$books = array($books = array(

array("title“ => “A",array("title“ => “A",

"author“ => “X"),"author“ => “X"),

array("title“ => “B",array("title“ => “B",

“ “author“ => “Y",author“ => “Y",

“ “price“ => 25)price“ => 25)

););

print_r($books);print_r($books);

Array( [0] => Array ( [title] => A [author] => X ) [1] => Array ( [title] => B [author => Y [price] => 25 ) )

1111PHP ArrayPHP Array

Nested LoopsNested Loops

for ($i=0; $i < count($books); $i++) {for ($i=0; $i < count($books); $i++) {

print "$i-th book is:";print "$i-th book is:";

while ( list($key, $value) = while ( list($key, $value) =

each($books[$i]) )each($books[$i]) )

print “ $key: $value";print “ $key: $value";

print "\n";print "\n";

}}

0-th book is: title: A author: X1-th book is: title: B author: Y price: 25

1212PHP ArrayPHP Array

String as an ArrayString as an Array

$myString = "My chars";$myString = "My chars";

echo "$myString\n"; echo "$myString\n";

echo "$myString[1]\n"; echo "$myString[1]\n";

My charsy

1313PHP ArrayPHP Array

Array functionsArray functions count(), sizeof() count(), sizeof() in_array()in_array() array_slice() array_slice() array_reverse()array_reverse() list( )list( ) Sorting FunctionsSorting Functions

sort(), rsort()sort(), rsort() asort(), arsort()asort(), arsort() ksort(), krsort()ksort(), krsort()

1414PHP ArrayPHP Array

count(array1)count(array1)andand sizeof(array1) sizeof(array1)

Returns the size of array Returns the size of array array1array1..

$animals = array ('dog', 'cat', 'fish');$animals = array ('dog', 'cat', 'fish');

echo count($animals);echo count($animals);

echo sizeof($animals);echo sizeof($animals);

33

1515PHP ArrayPHP Array

array array_reverse(array1)array array_reverse(array1)

Return an array with elements in reverse order.Return an array with elements in reverse order.

Array ( [0] = fish [1] = cat [2] = dog)

$animals = $animals =

array('dog', 'cat', 'fish');array('dog', 'cat', 'fish');

$reversed = $reversed = array_reverse($animals);array_reverse($animals);

print_r($reversed);print_r($reversed);

1616PHP ArrayPHP Array

array array_slicearray array_slice (array1, offset, length) (array1, offset, length)

Extract a slice of Extract a slice of lengthlength from from array1array1 starting starting atat

offsetoffset..

$array1 = $array1 =

array(1, 2, 3, 4, 5, 6);array(1, 2, 3, 4, 5, 6);

$subarray = $subarray =

array_slice($array1, 3, 2);array_slice($array1, 3, 2);

print_r($subarray);print_r($subarray);

Array( [0] = 4 [1] = 5)

1717PHP ArrayPHP Array

boolean in_array(value, array1)boolean in_array(value, array1)

Check if Check if valuevalue exists in exists in array1array1..

$animals = array('dog', 'cat', 'fish');$animals = array('dog', 'cat', 'fish');

echo in_array('cat', $animals);echo in_array('cat', $animals);

echo in_array(‘monkey', $animals);echo in_array(‘monkey', $animals);

1 (true) (false)

1818PHP ArrayPHP Array

void list(var1, var2, ...)void list(var1, var2, ...)

The elements of an array are copied into the The elements of an array are copied into the listlist

of variables of variables var1, var2,var1, var2, . . . . . .$animals = array('dog', 'cat', 'fish');$animals = array('dog', 'cat', 'fish');

list($a, $b, $c) = $animals;list($a, $b, $c) = $animals;

echo "$a, $b, $c";echo "$a, $b, $c";

dog, cat, fish

1919PHP ArrayPHP Array

sort(array)sort(array)andand rsort(array) rsort(array)

Sort the elements in an array in increasing Sort the elements in an array in increasing oror

decreasing order.decreasing order.

$animals = $animals =

array('dog', 'cat', 'fish');array('dog', 'cat', 'fish');

sort($animals);sort($animals);

print_r($animals);print_r($animals);

Array( [0] = cat [1] = dog [2] = fish )

2020PHP ArrayPHP Array

asort(array), arsort(array)asort(array), arsort(array)

Sort array by values and maintain index Sort array by values and maintain index associationassociation

$animals = $animals =

array('dog', 'cat', 'fish');array('dog', 'cat', 'fish');

asort($animals);asort($animals);

print_r($animals);print_r($animals);

Array( [1] = cat [0] = dog [2] = fish )

2121PHP ArrayPHP Array

ksort(array), krsort(array)ksort(array), krsort(array)

Sort array by keys.Sort array by keys.

$animals = $animals =

array('dog' => 15, array('dog' => 15,

'cat' => 8, 'cat' => 8,

'fish' => 2);'fish' => 2);

ksort($animals);ksort($animals);

print_r($animals);print_r($animals);

Array( [cat] => 8 [dog] => 15 [fish] => 12)