Download presentation
Presentation is loading. Please wait.
1
Web Systems Development (CSC-215)
Lecture 5: PHP Arrays Wednesday 14th February 2018
2
Class Test 1 Monday 19th February
Lab based – bring laptops or have desktop ready Lectures 1-6 Duration: 30 minutes
3
Adding Items to an Array
4
Adding Items to an Array
5
Retrieval
6
Associative Arrays
7
Example Useful for inserting/extracting information into/from HTML and XML
8
Assignment (using array keyword)
(more organized way)
9
foreach … as loop for arrays
10
For each with associative arrays
11
To modify, use as reference
12
Alternative, list & each function
13
Arrays Multidimensional
14
Direct Access
15
Multidimensional, Indexed
16
Array functions
17
Array Functions: is_array
18
Array Functions: count (counts number of elements)
For multi-dimensional 0 – only top level 1 – count all elements
19
Array Functions: sort (lowest to highest)
Sorts in place, returns TRUE or FALSE depending on success
20
Variations on sort
21
Array Functions: shuffle
In place, returns TRUE or FALSE
22
Array Functions: explode
23
Array Functions: explode
24
Array Functions: extract
Turns key/value pairs from an array into PHP variables For example, processing $_GET or $_POST variables A query string parameter q for example will get placed into a variable named $q
25
Avoid Overwriting $q becomes $fromget_q
27
Array Functions: compact (inverse of extract)
28
Array Functions: reset
Internal pointer in foreach
29
Array Functions: end Internal pointer in foreach
30
Lab Activity Imagine you are writing a version of the computer game Minesweeper. Use arrays to create and store a minefield on a 20 x 20 grid. Place ten mines randomly on the grid, then display the grid, using asterisks (* ) for the mines and dots (.) for the empty squares. (Hint: To return a random number between 0 and 19 inclusive, use rand( 0, 19 ) .) Your array should be populated using a nested loop (do not hard code like the chess example) Your script should display a new random configuration every time it is reloaded
31
Lab Activity Sample Output
32
Lecture content adapted from chapter 6 of Learning PHP, MySQL, JavaScript, CSS & HTML5, 3rd Edition, by Robin Nixon.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.