pic 40a - ucla department of mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfauthor...

31
Copyright 2011 Jukka Virtanen UCLA 1 PIC 40A 11/04/11 Lecture 18: PHP Arrays

Upload: others

Post on 29-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

Copyright 2011 Jukka Virtanen UCLA 1

PIC 40A

11/04/11

Lecture 18: PHP Arrays

Page 2: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 2

PHP Arrays

• Arrays have names that begin with $.

• Its elements consists of key-value pairs.

• Can have elements with positive integerkeys and/or string keys at the same time.

• Elements of an array need not be of thesame type.

Page 3: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 3

Declaring an array

2 ways to create an array:

1. Assign a value to an element of an array that does not exist$my_array[2] = 2006;

2. use the array construct$my_array = array(); # creates empty array

or

$my_array = array("Hello",2,TRUE);

Page 4: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 4

Example

$primes[0] = 2;$primes[1] = 3;$primes[] = 5; # 5 is stored in index 2.

Note: even if the script had a scalar variable called$primes before the assignment $primes[0]=2;$primes becomes an array.

Page 5: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 5

Using keys as indices

Arrays may be indexed either by ordinary non-negative integers or by“keys”.

Example:$ar = array("key1"=>"value 1", 67=>3, "a"=>10, 4=>"Hi");

We now have an array where instead of usual integer indices (0,1,...) Wehave keys indexing the slots.

"value 1" 3 10 "Hi"

By default ordinary indices are used:

$my_array = array(1,2,3,4); #keys 0,1,2,3

key1 67 a 4

Page 6: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 6

Accessing PHP Arrays

•String key 'Joe'

$password['Joe']="Hello!";$pw = $password['Joe'];

•Number Key 2

$password[2]="Hello!";

Warning: "2" and 2 are the same key!

Page 7: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 7

Printing array elements.

You can interpolate array element variables, butyou have to be careful!

//These do not work!print("Passwd:$password[‘Joe’].");print "Passwd:$password["Joe"].";

//This is right.print "Passwd:$password[Joe]";

Page 8: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 8

Getting Array Keys and Values as Arrays with Integer Keys

•Use the array_keys and array_valuesfunctions

$table=array("Fry"=>"slurm", "Leela"=>"PlanEx3011");

$usernames = array_keys($table);$passwds= array_values($table);

Page 9: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 9

Example continued

$usernames[0] is "Fry"$usernames[1] is "Leela"$passwds[0] is "Slurm"$passwds[1] is "PlanEx3011"

Page 10: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 10

PHP Array functions

•unsetDeletes the contents of a scalar variable, an arrayentry, or an entire array.

•countReturns the number of elements in an array.

•sizeofThis is same as count.

•is_arrayReturns TRUE if the argument is an array.

Page 11: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 11

Example

$list = array(1,2,3,4); #keys 0,1,2,3

unset($list[2]);Now $list has keys 0,1,3 and elements 1,2,4

unset($list); Now $list is NULL

$a = 5;unset($a);

Now $a is NULL

Page 12: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 12

explode

explode is JS equivalent of split. It takes anargument of a string and a delimiter and returns anarray consisting of substrings of the string.

Example:$str = "a:b:c:d";$ar = explode(":",$str);

a b c d$str

$ar

Page 13: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 13

implode

implode does the inverse. It takes an arrayand returns a string where the entries areappended together using a delimiter.

Example:

$str = implode("^",$ar");

$str has the value: "a^b^c^d".

Page 14: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 14

Accessing array elements in sequentialorder

•Every array has an internal pointer whichpoints to the "current" element of the array.

•The "current" pointer is initialized to the firstelement of the array when the array is created

•To access and dereference the "current"pointer of an array, use the current function

Page 15: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 15

Current Pointer Example

$snacks = array("chips", "pretzels", "candy");$snack = current($snacks);print("The first snack is $snack");

Output: The first snack is chips

Page 16: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 16

Accessing array elements in sequential order

The next function moves an array's "current"pointer to the next element in the array andthen dereference it.

The prev function moves the "current" pointerto the previous element in the array and thendereference it.

Page 17: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 17

Moving Current Pointer Example

$snacks = array("chips", "pretzels", "candy");

$snack = next($snacks);print("The 2nd snack is $snack");

$snack = prev($snacks);print("The 1st snack is $snack");

# The 2nd snack is pretzels# The 1st snack is chips

Page 18: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 18

More about next

Current pointer is moved with the next function, whichmoves the pointer to the next array element and returnsthe value of that element. If the current pointer is pointingto the end of the array the function returns FALSE.

Example:$snacks = array("chips", "pretzels", "candy");$snack = current($snacks);print ("$snack <br/>");while($snack = next($snacks))print("$snack <br/>");

Page 19: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 19

Warning about next

If an element of the array has a value FALSE theloop in the preceding example would end beforewe reached the end of the array.

One may avoid this problem by using the eachfunction.

Page 20: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 20

More Array Traversing

reset function•Moves an array's "current" pointer to the firstelement in the array and then dereferences it.

end function•moves "current" to the last element in thearrayand then dereferences it.

Page 21: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 21

Moving Current Pointer Example

$snacks = array("chips", "pretzels", "candy");

$first = reset($snacks);print("The first snack is $first");

$last = end($snacks);print("The last snack is $last");

# The first snack is chips# The last snack is candy

Page 22: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 22

each function

• Returns a two element array consisting ofthe key and the value of the "current"element in the array.

• The keys of the two element array are "key"and "value".

• It then moves the "current" pointer to thenext element of the array or FALSE if thereisn't one.

Page 23: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 23

each Example

$snacks = array("abe" => "chips", "abby" => "pretzels","darin" => "candy");

while($order= each($snacks)){

$partier = $order["key"];$snack = $order["value"];print("$partier wants $snack");

}

# abe wants chips# abby wants pretzels# darin wants candy

Page 24: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 24

Other PHP Array Functions

•key($array)–returns the key of the "current" element of the array

•in_array($value, $array, $strict)–returns TRUE if the given $value exists in the array$array. If $strict is TRUE,then the type of $valuemust also match the type of the element in the array.

Page 25: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 25

Using an array as a stack

•array_push($array, $e1, $e2,..)Places the elements $e1,$e2 at the end of the array$array and returns the new number of elements inthearray.

•array_pop($array)Removes and returns the last element of the array$array,or NULL if the array is empty.

Page 26: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 26

More PHP control structures

•foreach loop

Used to process all the elements of an array. Thereare two forms of foreach:

1.foreach($array as $value) {}2.foreach($array as $key => $value){}

Page 27: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 27

foreach Example

$snacks = array("abe" => "chips", "abby" => "pretzels","darin" => "candy");

foreach($snacks as $val){

print("$val<br/>");}

# Output is:# chips# pretzels# candy

Page 28: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 28

foreach Example

$snacks = array("abe" => "chips", "abby" => "pretzels","darin" => "candy");

foreach($snacks as $partier => $snack){

print("$partier wants $snack<br/>");}

# Output is:# abe wants chips# abby wants pretzels# darin wants candy

Page 29: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 29

Sorting PHP Arrays

sort($array)

•Sorts the values in $array, but replaces thekeys by 0,1,2,3, etc...

•String values appear first in alphabetical orderfollowed by the numeric values in ascendingorder.

Page 30: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 30

Sorting PHP Arrays

asort($array)Same as sort($array), but key-valueassociations are preserved.

ksort($array)Sorts $array by its keys, rather than by itsvalues. Key-value associations are preserved.

Page 31: PIC 40A - UCLA Department of Mathematicsmath.ucla.edu/~virtanen/40a.1.16f/schedule/lec18.pdfAuthor Jukka Virtanen Created Date 11/4/2011 11:58:19 AM

11/04/11 Copyright Jukka Virtanen 2011 31

Sorting PHP Arrays

•rsort($array)•arsort($array)•krsort($array)

These functions behave the same as sort,asort,and ksort respectively, but sort inthe reverse order.