Pic 40A: Lecture 18: PHP Arrays
Pic 40A: Lecture 18: PHP Arrays
PIC 40A
11/04/11
Lecture 18: PHP Arrays
11/04/11
Copyright Jukka Virtanen 2011
2
PHP Arrays
Arrays hae na!es that "egin #ith $%
Its e&e!ents consists o' key(a&ue pairs%
Can hae e&e!ents #ith positie integer
keys an)*or string keys at the sa!e ti!e%
+&e!ents o' an array nee) not "e o' the
sa!e type%
11/04/11
Copyright Jukka Virtanen 2011
,
-ecå an array
2 #ays to create an array:
1% Assign a a&ue to an e&e!ent o' an array that )oes not e.ist
$my_array[2] = 2006;
2% use the array construct
$my_array = array(); # creates empty array
or
$my_array = array("Hello",2,TRUE);
11/04/11
Copyright Jukka Virtanen 2011
4
+.a!p&e
$prmes[0] = 2;
$prmes[!] = ";
$prmes[] = #; # # s store$ % %$e& 2'
/ote: een i' the script ha) a sca&ar aria"&e ca&&e)
$prmes "e'ore the assign!ent $prmes[0]=2;
$prmes "eco!es an array%
11/04/11
Copyright Jukka Virtanen 2011
0
Using keys as in)ices
Arrays !ay "e in)e.e) either "y or)inary non(negatie integers or "y
1keys2%
+.a!p&e:
$ar = array("(ey!"=)"*al+e !", 6,=)", "a"=)!0, -=)"H");
3e no# hae an array #here instea) o' usua& integer in)ices 40515%%%6 3e
hae keys in)e.ing the s&ots%
"*al+e !" " !0 "H"
7y )e'au&t or)inary in)ices are use):
$my_array = array(!,2,",-); #(eys 0,!,2,"
key1
89 a 4
11/04/11
Copyright Jukka Virtanen 2011
8
Accessing PHP Arrays
:;tring key ./oe.
$pass0or$[./oe.]="Hello1";
$p0 = $pass0or$[./oe.];
:/u!"er <ey 2
$pass0or$[2]="Hello1";
Warning: "2" and 2 are the same key!
11/04/11
Copyright Jukka Virtanen 2011
9
Printing array e&e!ents%
=ou can interpo&ate array e&e!ent aria"&es5 "ut
you hae to "e care'u&>
22T3ese $o %ot 0or(1
pr%t("4ass0$5$pass0or$[6/oe7]'");
pr%t "4ass0$5$pass0or$["/oe"]'";
22T3s s r83t'
pr%t "4ass0$5$pass0or$[/oe]";
11/04/11
Copyright Jukka Virtanen 2011
8
?etting Array <eys an) Va&ues as
Arrays #ith Integer <eys
:Use the array_(eys an) array_*al+es
'unctions
$ta9le=array(":ry"=)"sl+rm", ";eela"=)"4la%E&"0!!");
$+ser%ames = array_(eys($ta9le);
$pass0$s= array_*al+es($ta9le);
11/04/11
Copyright Jukka Virtanen 2011
@
+.a!p&e continue)
$+ser%ames[0] is ":ry"
$+ser%ames[!] is ";eela"
$pass0$s[0] is "<l+rm"
$pass0$s[!] is "4la%E&"0!!"
11/04/11
Copyright Jukka Virtanen 2011
10
PHP Array 'unctions
unset
-e&etes the contents o' a sca&ar aria"&e5 an array
entry5 or an entire array%
count
Aeturns the nu!"er o' e&e!ents in an array%
sizeof
Bhis is sa!e as count.
is_array
Aeturns TRUE i' the argu!ent is an array%
11/04/11
Copyright Jukka Virtanen 2011
11
+.a!p&e
$lst = array(!,2,",-); #(eys 0,!,2,"
+%set($lst[2]);
/o# $lst has keys 0,!," an) e&e!ents !,2,-
+%set($lst);
/o# $lst is =U;;
$a = #;
+%set($a);
/o# $a is =U;;
11/04/11
Copyright Jukka Virtanen 2011
12
e&plo$e
e&plo$e is J; eCuia&ent o' sp&it% It takes an
argu!ent o' a string an) a )e&i!iter an) returns an
array consisting o' su"strings o' the string%
+.a!p&e:
$str D Ea:":c:)EF
$ar D e.p&o)e4E:E5$str6F
a 9 c $
$str
$ar
11/04/11
Copyright Jukka Virtanen 2011
1,
mplo$e
mplo$e )oes the inerse% It takes an array
an) returns a string #here the entries are
appen)e) together using a )e&i!iter%
+.a!p&e:
$str = mplo$e(">",$ar");
$str has the a&ue: "a>9>c>$"%
11/04/11
Copyright Jukka Virtanen 2011
14
Accessing array e&e!ents in seCuentia&
or)er
:+ery array has an interna& pointer #hich
points to the EcurrentE e&e!ent o' the array%
:Bhe EcurrentE pointer is initia&iGe) to the 'irst
e&e!ent o' the array #hen the array is create)
:Bo access an) )ere'erence the EcurrentE
pointer o' an array5 use the c+rre%t 'unction
11/04/11
Copyright Jukka Virtanen 2011
10
Current Pointer +.a!p&e
$s%ac(s = array("c3ps", "pret?els", "ca%$y");
$s%ac( = c+rre%t($s%ac(s);
pr%t("T3e @rst s%ac( s $s%ac(");
Hutput: Bhe 'irst snack is chips
11/04/11
Copyright Jukka Virtanen 2011
18
Accessing array e&e!ents in seCuentia&
or)er
Bhe %e&t 'unction !oes an arrayIs EcurrentE
pointer to the ne.t e&e!ent in the array an)
then )ere'erence it%
Bhe pre* 'unction !oes the EcurrentE pointer
to the preious e&e!ent in the array an) then
)ere'erence it%
11/04/11
Copyright Jukka Virtanen 2011
19
Joing Current Pointer +.a!p&e
$s%ac(s = array("c3ps", "pret?els", "ca%$y");
$s%ac( = next($snacks);
pr%t("T3e 2%$ s%ac( s $s%ac(");
$s%ac( = prev($snacks);
pr%t("T3e !st s%ac( s $s%ac(");
# T3e 2%$ s%ac( s pret?els
# T3e !st s%ac( s c3ps
11/04/11
Copyright Jukka Virtanen 2011
18
Jore a"out %e&t
Current pointer is !oe) #ith the %e&t 'unction5 #hich
!oes the pointer to the ne.t array e&e!ent an) returns
the a&ue o' that e&e!ent% I' the current pointer is pointing
to the en) o' the array the 'unction returns :A;<E%
+.a!p&e:
$s%ac(s = array("c3ps", "pret?els", "ca%$y");
$s%ac( = c+rre%t($s%ac(s);
pr%t ("$s%ac( B9r2)");
03le($s%ac( = %e&t($s%ac(s))
pr%t("$s%ac( B9r2)");
11/04/11
Copyright Jukka Virtanen 2011
1@
3arning a"out %e&t
I' an e&e!ent o' the array has a a&ue :A;<E the
&oop in the prece)ing e.a!p&e #ou&) en) "e'ore
#e reache) the en) o' the array%
Hne !ay aoi) this pro"&e! "y using the eac3
'unction%
11/04/11
Copyright Jukka Virtanen 2011
20
Jore Array Braersing
reset 'unction
:Joes an arrayIs EcurrentE pointer to the 'irst
e&e!ent in the array an) then )ere'erences it%
e%$ 'unction
:!oes EcurrentE to the &ast e&e!ent in the
array
an) then )ere'erences it%
11/04/11
Copyright Jukka Virtanen 2011
21
Joing Current Pointer +.a!p&e
$s%ac(s = array("c3ps", "pret?els", "ca%$y");
$@rst = reset($s%ac(s);
pr%t("T3e @rst s%ac( s $@rst");
$last = e%$($s%ac(s);
pr%t("T3e last s%ac( s $last");
# T3e @rst s%ac( s c3ps
# T3e last s%ac( s ca%$y
11/04/11
Copyright Jukka Virtanen 2011
22
eac3 'unction
Aeturns a t#o e&e!ent array consisting o'
the key an) the a&ue o' the EcurrentE
e&e!ent in the array%
Bhe keys o' the t#o e&e!ent array are EkeyE
an) Ea&ueE%
It then !oes the EcurrentE pointer to the
ne.t e&e!ent o' the array or :A;<E i' there
isnIt one%
11/04/11
Copyright Jukka Virtanen 2011
2,
eac3 +.a!p&e
$s%ac(s = array("a9e" =) "c3ps", "a99y" =) "pret?els",
"$ar%" =) "ca%$y");
03le($or$er= each($snacks))
C
$parter = $order["key"];
$s%ac( = $order["value"];
pr%t("$parter 0a%ts $s%ac(");
D
# a9e 0a%ts c3ps
# a99y 0a%ts pret?els
# $ar% 0a%ts ca%$y
11/04/11
Copyright Jukka Virtanen 2011
24
Hther PHP Array Kunctions
(ey($array)
Lreturns the key o' the EcurrentE e&e!ent o' the array
%_array($*al+e, $array, $strct)
Lreturns BAU+ i' the gien $*al+e e.ists in the array
$array. I' $strct is TRUE,then the type o' $*al+e
!ust a&so !atch the type o' the e&e!ent in the array%
11/04/11
Copyright Jukka Virtanen 2011
20
Using an array as a stack
array_p+s3($array, $e!, $e2,'')
4&aces the e&e!ents $e!,$e2 at the en) o' the array
$array an) returns the ne# nu!"er o' e&e!ents in
the
array%
array_pop($array)
Ae!oes an) returns the &ast e&e!ent o' the array
$array,or =U;; i' the array is e!pty%
11/04/11
Copyright Jukka Virtanen 2011
28
Jore PHP contro& structures
@oreac3 &oop
Use) to process a&& the e&e!ents o' an array% Bhere
are t#o 'or!s o' @oreac3:
!'@oreac3($array as $value) CD
2'@oreac3($array as $key =) $value)CD
11/04/11
Copyright Jukka Virtanen 2011
29
'oreach +.a!p&e
$s%ac(s = array("a9e" =) "c3ps", "a99y" =) "pret?els",
"$ar%" =) "ca%$y");
@oreac3($s%ac(s as $*al)
C
pr%t("$*alB9r2)");
D
# E+tp+t s5
# c3ps
# pret?els
# ca%$y
11/04/11
Copyright Jukka Virtanen 2011
28
'oreach +.a!p&e
$s%ac(s = array("a9e" =) "c3ps", "a99y" =) "pret?els",
"$ar%" =) "ca%$y");
@oreac3($s%ac(s as $parter =) $s%ac()
C
pr%t("$parter 0a%ts $s%ac(B9r2)");
D
# E+tp+t s5
# a9e 0a%ts c3ps
# a99y 0a%ts pret?els
# $ar% 0a%ts ca%$y
11/04/11
Copyright Jukka Virtanen 2011
2@
;orting PHP Arrays
sort($array)
:;orts the a&ues in $array5 "ut rep&aces the
keys "y 051525,5 etc%%%
:;tring a&ues appear 'irst in a&pha"etica& or)er
'o&&o#e) "y the nu!eric a&ues in ascen)ing
or)er%
11/04/11
Copyright Jukka Virtanen 2011
,0
;orting PHP Arrays
asort($array)
;a!e as sort($array)5 "ut key(a&ue
associations are presere)%
(sort($array)
;orts $array "y its keys5 rather than "y its
a&ues% <ey(a&ue associations are presere)%
11/04/11
Copyright Jukka Virtanen 2011
,1
;orting PHP Arrays
Frsort($array)
Farsort($array)
F(rsort($array)
Bhese 'unctions "ehae the sa!e as sort,
asort,an) (sort respectie&y5 "ut sort in
the reerse or)er%