Presentation is loading. Please wait.

Presentation is loading. Please wait.

NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION.

Similar presentations


Presentation on theme: "NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION."— Presentation transcript:

1 NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION

2 WHAT IS AN ARRAY? An array refers to an ordered list of elements (numbers, strings or objects) that will follow a specific pattern and are stored in one value. There are typically two types of arrays that sometimes have different names but follow specific rules. NUMERIC One is called a NUMERIC or indexed array. This is where a numeric value is used to identify or store the element as well as call the element. ASSOCIATIVE The other is an ASSOCIATIVE array. These elements are identified and called by a string. MULTI DIMENSIONAL There are MULTI DIMENSIONAL arrays that are either numeric or associative as well.

3 THE PARAMETERS OF A NUMERIC ARRAY: Start This is the first value of the sequence. End The sequence is ended upon reaching the end value. Step If a step value is given, it will be used as the increment between elements in the sequence. Step should be given as a positive number. If not specified, step will default to 1. Return The return will display the elements from start to end NUMERIC ARRAYS NUMERIC ARRAYS can store numbers, strings and any object but their index will be represented by numbers. By default, an array index starts from zero [0].

4 HOW TO WRITE A PHP ARRAY: It is important to declare an array before you use it. The following are two examples of an array with five elements: <?php // first method to create a numeric array $numbers = array(1, 2, 3, 4, 5); for each($numbers as $value) { echo "Value is $value "; } ?> <?php // second method to create a numeric array $numbers [0] = "one"; $numbers [1] = "two"; $numbers [2] = "three"; $numbers [3] = "four"; $numbers [4] = "five"; for each($numbers as $value) { echo "Value is $value "; } ?> This will show as: Value is 1 Value is 2 Value is 3 Value is 4 Value is 5 This will show as: Value is one Value is two Value is three Value is four Value is five

5 GETTING THE LENGTH OF AN ARRAY: <?php $numbers = array ("1", "2", "3", "4", "5"); echo count ($numbers); ?> The Result 5 Array index out of bounds This run-time error is reported whenever a formula references an array with an index that is outside the valid boundaries of the array. count() function The count() function is used to return the length (the number of elements) in an array. The number of elements is called the length of the array. The last element in the array has an index one less than the array length. To avoid bound errors, you will want to know how many elements are in an array. The length field returns the number of elements Example of a Bound Error : $array a[10] $array x = 10 $a[9] = 3; ok $a[x] = 4; 10 is not within the range 0..9

6 HOW TO LOOP THROUGH AN ARRAY: <?php $numbers = array("1", "2", "3", "4", "5"); $arrlength = count ($numbers); for($x = 0; < $arrlength; $x++) { echo $numbers[$x]; echo " "; } ?> Note: numbersvaluevalues The words “numbers”, “value” and “values” that have been used in the previous examples, can be replaced by any identifier. Loops can execute a block of code a number of times. Loops are handy, if you want to run the same code over and over again, each time with a different value.

7 ADDITIONAL EXAMPLES: simple array // simple array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) { echo $number; } Note: Character sequence values Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used. step // The step parameter // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) { echo $number; } character sequences // Usage of character sequences // array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i') foreach (range('a', 'i') as $letter) { echo $letter; } letter array // letter array('c', 'b', 'a'); foreach (range('c', 'a') as $letter) { echo $letter; }

8 DISPLAY ELEMENTS RANDOMLY: srand() The PHP srand() function is used to generate a random number or element. This function can generate numbers with-in a given range. The random number generator should be seeded to prevent a regular pattern of numbers being generated. srand() This is achieved using the srand() function that specifies the seed number as its argument. Shown above - Example 1 <?php srand( microtime() * 1000000 ); $num = rand( 1, 4 ); switch( $num ) { case 1: $image_file = "/home/images/alfa.jpg"; break; case 2: $image_file = "/home/images/ferrari.jpg"; break; case 3: $image_file = "/home/images/jaguar.jpg"; break; case 4: $image_file = "/home/images/porsche.jpg"; break; } echo "Random Image : "; ?> This example shows how to display images in an array randomly. srand — Seed the random number generator Example 2

9 USING HTML FORMS: <?php if( $_POST["name"] || $_POST["age"] ) { echo "Welcome ". $_POST['name']. " "; echo "You are ". $_POST['age']. " years old."; exit(); } ?> " method="POST"> Name: Age: Note: This is not an array function but may be useful information that is relevant to this class especially since we are learning database format..

10 RESOURCES: http://php.net/manual/en/function.range.php http://php.net/manual/en/function.array-splice.php http://webcheatsheet.com/php/arrays.php http://www.w3schools.com/js/js_loop_for.asp http://www.tutorialspoint.com/php/php_arrays.htm http://www.tutorialspoint.com/php/php_web_concepts.htm http://www.informit.com/articles/article.aspx?p=1969707 http://www.ultrafractal.com/help/writing/reference/errors/arrayindexoutofbounds.html http://thismitchellfamily.com/336presentation/index.php?action=return Thank you for your time. I hope this helps you understand arrays. Have a great day.


Download ppt "NUMERIC ARRAYS DEBBI HAMNER CIT 336 TEACHING PRESENTATION."

Similar presentations


Ads by Google