Introduction to PHP - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to PHP

Description:

Introduction to PHP Chapter 9 Arrays Array Definition Arrays are created with the array() constructor: $ArrayName = array([(mixed) ]) By default, PHP arrays ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 18
Provided by: David11090
Learn more at: https://instesre.org
Category:

less

Transcript and Presenter's Notes

Title: Introduction to PHP


1
Introduction to PHP Chapter 9
  • Arrays

2
Array Definition
  • Arrays are created with the array() constructor
  • ArrayName array((mixed))
  • By default, PHP arrays have user-defined index
    (key) values
  • a array(key1 gt value1, key2 gt value2,
  • key3 gt value3,)
  • Keys can be numbers, characters, or strings.
    Numerical keys can start with any value, not just
    0.

3
Document 9.1. (keyedArray.php) lt?php // Create an
array with user-specified keys... echo 'ltbr /gtA
keyed arrayltbr /gt'stuff array('mine' gt
'BMW', 'yours' gt 'Lexus', 'ours' gt
'house')foreach (stuff as key gt val)
echo 'stuff' . key . ' '. val . 'ltbr
/gt' ?gt A keyed arraystuffmine
BMWstuffyours Lexusstuffours house
4
Using for... loopdavidappleXenaSueUsing
implied keys with foreach... loopa0
davida1 applea2 Xenaa3 SueAn
array with keys starting at an integer other
than 0BMWLexushouseA keyed array with
consecutive character keys...BMWLexushouse
If keys are not supplied, the default is integer
keys starting at 0.
Document 9.2. (ConsecutiveKeyArray.php) lt?phpa
array('david','apple','Xena','Sue') echo
"Using for... loopltbr /gt"for (i0
iltsizeof(a) i) echo ai . 'ltbr
/gt'echo "Using implied keys with foreach...
loopltbr /gt"foreach (a as i gt x) echo
'a' . i . ' ' . x . 'ltbr /gt'echo "An
array with keys starting at an integer other than
0ltbr /gt"negKey array(-1 gt 'BMW', 'Lexus',
'house')for (i-1 ilt2 i) echo
negKeyi . 'ltbr /gt'echo 'A keyed array with
consecutive character keys...ltbr /gt'stuff
array('a' gt 'BMW', 'b' gt 'Lexus', 'c' gt
'house')for (i'a' iltsizeof(stuff) i)
echo stuffi . 'ltbr /gt'?gt
5
Document 9.3. (base_1Array.php) lt?php echo 'ltbr
/gtA keyed array with indices starting at 1...ltbr
/gt'a array(1 gt 63.7, 77.5, 17,
-3) foreach(a as key gt val) echo
'a' . key . ' '. val . 'ltbr /gt' for
(i1 iltsizeof(a) i) echo ai . 'ltbr
/gt' ?gt A keyed array with indices starting at
1... a1 63.7a2 77.5a3 17a4
-363.777.517-3
6
Defining a 2-D array with default integer keys
(starting at 0)
Document 9.4. (two-D.php) lt?phpecho 'ltbr /gtA 2-D
arrayltbr /gt'a array( 0 gt array(1,2,3,4),
1 gt array(5,6,7,8), 2 gt array(9,10,11,12),
3 gt array(13,14,15,16), 4 gt
array(17,18,19,20))n_rcount(a) echo '
rows ' . n_r . 'ltbr /gt'n_ccount(a0)
echo ' columns ' . n_c . 'ltbr /gt'for (r0
rltn_r r) for (c0 cltn_c c)
echo arc . ' ' echo 'ltbr /gt'?gt
A 2-D array rows 5 columns 41 2 3 4 5 6
7 8 9 10 11 12 13 14 15 16 17 18 19 20
7
A basic PHP sort still has problems with
upper/lower case strings
Original arraydavidappleXenaSueSorted
arraySueXenaappledavid
Document 9.5. (sort1.php) lt?php// Create and
sort an arraya array('david','apple',Sue',X
ena') echo 'Original arrayltbr /gt'for (i0
iltsizeof(a) i) echo ai . 'ltbr
/gt'sort(a) echo 'Sorted arrayltbr /gt'for
(i0 iltsizeof(a) i) echo ai . 'ltbr
/gt' ?gt
Document 9.6. (sort2.php) lt?php aarray(3.3,-13,
-0.7,14.4) sort(a) for (i0 iltsizeof(a)
i) echo ai . 'ltbr /gt'?gt -13-0.73.3
14.4
but not with numbers (unlike JavaScript)
8
Fix the sort function by using a user-defined
compare function.
Document 9.7. (sort3.php) lt?phpfunction
compare(x,y) return strcasecmp(x,y)
// Create and sort an arraya
array('david','apple','Xena','Sue') echo
'Original arrayltbr /gt'for (i0
iltsizeof(a) i) echo ai . 'ltbr
/gt'echo 'Sorted array with user-defined
comparisons of elementsltbr /gt'
usort(a,"compare") for (i0 iltsizeof(a)
i) echo ai . 'ltbr /gt' ?gt
Original arraydavidappleXenaSue Sorted
array with user-defined comparisons of
elementsappledavidSueXena
9
Stacks, Queues, and Line Crashers
  • For manipulating these data structures, assume
    arrays with integer indices starting at 0. Then
    we can use
  • array_pop() remove last (newest) element
  • array_shift() remove first (oldest) element
  • array_push() add new element(s) to new end of
    array
  • array_unshift() add new element(s) to old end
    of array

10
Document 9.10 (arrayPop.php) lt?phpstack
array("orange", "banana", "apple",
"lemon")fruitarray_pop(stack)print_r(stack
)?gt Array ( 0 gt orange 1 gt banana 2 gt
apple)
Document 9.11 (arrayPush.php) lt?phpstack
array("red", "grn")n array_push(stack,
"blu", "wh")print_r(stack)stack
"blk"printf("ltbr /gtultbr /gt",n)print_r(stack
) printf("ltbr /gtultbr /gt",sizeof(stack))?gt Arr
ay ( 0 gt red 1 gt grn 2 gt blu 3 gt wh )
4Array ( 0 gt red 1 gt grn 2 gt blu 3
gt wh 4 gt blk ) 5
11
Document 9.12 (arrayShift.php) lt?phpqueue
array("orange", "banana", "raspberry",
"mango")print_r(queue)rottenFruit
array_shift(queue)echo 'ltbr /gt' .
rottenFruitecho 'ltbr /gt' . count(queue)?gt Ar
ray ( 0 gt orange 1 gt banana 2 gt
raspberry 3 gt mango )orange 3
Document 9.13 (arrayUnshift.php) lt?phpa
array("orange", "banana", "raspberry",
"mango")print_r(a)array_unshift(a,"papaya","
mangosteen")echo 'ltbr /gt' . count(a) . 'ltbr
/gt'print_r(a)?gt Array ( 0 gt orange 1 gt
banana 2 gt raspberry
12
The quadratic formula revisited
Document 9.9a (quadrat_2.htm) lthtmlgtltheadgtlttitle
gtSolving the Quadratic Equationlt/titlegtlt/headgtltb
odygtltform method"post" action"quadrat_2.php"gtE
nter coefficients for axltsupgt2lt/supgt bx c
0ltbr /gta ltinput type"text" value"1"
name"coeff0" /gt (must not be 0)ltbr /gtb
ltinput type"text" value"2" name"coeff1"
/gtltbr /gtc ltinput type"text" value"-8"
name"coeff2" /gtltbr /gtltbr /gtltinput
type"submit" value"click to get roots..."
/gtlt/formgtlt/bodygtlt/htmlgt
Store coefficients in an array.
13
Quadratic formula (cont.)
Document 9.9b (quadrat_2.php) lt?phpvar_dump(_POS
T"coeff")echo "ltbr /gt" coefficientArrayarra
y_keys(_POST"coeff")a _POST"coeff"coef
ficientArray0b_POST"coeff"coefficientA
rray1c_POST"coeff"coefficientArray2
d bb - 4.acif (d 0) r1
b/(2.a) r2 "undefined"else if (d lt
0) r1 "undefined" r2
"undefined"else r1 (-b sqrt(bb -
4.ac))/2./ar2 (-b - sqrt(bb -
4.ac))/2./aecho "r1 " . r1 . ", r2
" . r2 ?gt array(3) 0gt string(1) "1"
1gt string(1) "2" 2gt string(2) "-8" r1
2, r2 -4
14
Document 9.10a (CloudObs.htm) lthtmlgtltheadgtlttitle
gtCloud Observationslt/titlegtlt/headgtltbody
bgcolor"aaddff"gtlth1gtCloud Observationslt/h1gtltst
ronggt Cloud Observations lt/stronggt(Select as many
cloud types as observed.)ltbr /gtltform
method"post" action"CloudObs.php" /gtlttablegt
lttrgt lttdgtltstronggtHighlt/stronggt lt/tdgt lttdgt
ltinput type"checkbox" name"high"
value"Cirrus" /gt Cirruslt/tdgt lttdgt
ltinput type"checkbox" name"high"
value"Cirrocumulus" /gt Cirrocumulus lt/tdgt
lttdgt ltinput type"checkbox" name"high"
value"Cirrostratus" /gt Cirrostratus
lt/tdgtlt/trgt lttrgt lttd colspan"4"gtlthr
noshade color"black" /gt lt/tdgtlt/trgt lttrgt
lttdgt ltstronggtMiddlelt/stronggt lt/tdgt lttdgt
ltinput type"checkbox" name"mid"
value"Altostratus" /gt Altostratus lt/tdgt lttdgt
ltinput type"checkbox" name"mid"
value"Altocumulus" /gt Altocumuluslt/tdgtlt/trgt
lttrgt lttd colspan"4"gtlthr noshade
color"black" /gt lt/tdgtlt/trgt lttrgt lttdgt
ltstronggtLowlt/stronggtlt/tdgt lttdgt ltinput
type"checkbox" name"low" value"Stratus" /gt
Stratuslt/tdgt lttdgt ltinput
type"checkbox" name"low"
value"Stratocumulus" /gt Stratocumuluslt/tdgt
lttdgt ltinput type"checkbox" name"low"
value"Cumulus" /gt Cumulus lt/tdgtlt/trgt
lttrgt lttd colspan"4"gtlthr noshade
color"black" /gt lt/tdgtlt/trgt lttrgt
lttdgt ltstronggtRain-Producing lt/stronggt lt/tdgt
lttdgt ltinput type"checkbox" name"rain"
value"Nimbostratus" /gt
Nimbostratuslt/tdgt lttdgt ltinput
type"checkbox" name"rain"
value"Cumulonimbus" /gt Cumulonimbus
lt/tdgtlt/trgtlt/tablegtltinput type"submit"
value"Click to process..." /gtlt/formgtlt/bodygtlt/h
tmlgt
15
Document 9.10b (CloudObs.php) lt?php high
_POST"high" n count(high) echo
"For high clouds, you observedltbr /gt" for
(i0 iltn i) echo highi .
"ltbrgt" mid _POST"mid" n
count(mid) echo "for mid clouds, you
observedltbr /gt" for (i0 iltn i)
echo midi . "ltbr /gt" low
_POST"low" n count(low) echo
"for low clouds, you observedltbr /gt" for
(i0 iltn i) echo lowi . "ltbr
/gt" rain _POST"rain" n
count(rain) echo "for precipitating
clouds, you observedltbr /gt" for (i0 iltn
i) echo raini . "ltbr /gt"?gt
PHP builds an array containing only those boxes
which have been checked. (Easier than JavaScript!)
For high clouds, you observed CirrocumulusCirros
tratusfor mid clouds, you observedfor low
clouds, you observedfor precipitating clouds,
you observedCumulonimbus
16
Write a PHP application that reads scores between
0 and 100 (possibly including both 0 and 100)
and creates a histogram array whose elements
contain the number of scores between 0 and 9,
10 and 19, etc. The last box in the histogram
includes scores between 90 and 100. Use a
function to generate the histogram.
The data file looks like this (integers between
0 and 100) 73 77 86 17 18
Your output should look something like
this Number of scores 39building
histogram...size of histogram array 10h0
1h1 5h2 2h3 5h4 5h5
1h6 3h7 3h8 8h9 6 of
entries 39
17
Document 9.13 (cardShuffle.php)   lt?php deck
array() for (i 0 ilt52 i)
deckii1 echo decki." " echo
"ltbr /gt" for (i0 ilt52 i)
jrand(0,51) savedecki deckideck
j deckjsave for (i0 ilt52
i) echo decki." " echo "ltbr /gt"
sort(deck) echo "Resort deck...ltbr /gt" for
(i0 ilt52 i) echo decki." "
echo "ltbr /"?gt  
Write a PHP application that will shuffle a deck
of 52 cards. Represent the cards as an array
of integers having values from 1 to 52. After
the results of shuffling this deck are
displayed, sort the deck in ascending order and
display it again.
Write a Comment
User Comments (0)
About PowerShow.com