Introduction The nextand lastvariable type youll learn is
Introduction The next—and last—variable type you’ll learn is the array. Arrays are significantly different from numbers or strings, and you can’t make the most of programming in PHP without understanding them. In This Chapter ● What Is an Array? ● Creating an Array ● Adding Items to an Array ● Accessing Array Elements ● Creating Multidimensional Arrays ● Sorting Arrays ● Transforming Between Strings and Arrays ● Creating an Array from a Form ● Review and Homework
What is an Array Whereas numbers and strings are scalar variables— meaning they always have only a single value—an array is a collection of multiple values assembled into one overriding variable. You can greatly simplify matters by placing your entire list into one array (say, $items), which contains everything you need. For example, if you wanted to create a grocery list using strings, your code would look something like this: As an array, your list can be added to, sorted, searched, and so forth. $item 1 = 'apples'; $item 2 = 'bananas'; $item 3 = 'oranges';
Syntactical rules for arrays ● They begin with a dollar sign. ● They continue with a letter or underscore. ● They finish with any combination of letters, numbers, ● or the underscore. An element consists of an index or key—the two words can be used interchangeably—and a value.
Creating an Array $name = array(); # create an empty array (length 0) $name = array(value 0, value 1, . . . , value. N); $name[index] # get element value $name[index] = value; # set element value $name[] = value; # append value to the previous array $a = array(); # empty array (length 0) $a[0] = 23; # stores 23 at index 0 (length 1) $a 2 = array("some", "strings", "in", "array"); $a 2[] = "Ooh!"; # add string to end (at index 5) • to append, use bracket notation without specifying an index • element type is not specified; can mix types
Creating an Array Its syntax is $list = array('apples', 'bananas', 'oranges'); // index starts with 0 If you desire, you can assign the index when using array(): $list = array(1 => 'apples', 2 => ➝'bananas', 3 => 'oranges'); // index starts with 1 Better structure:
Finally, the index value you specify doesn’t have to be a number—you can use strings instead.
$arr=array(range(a, z)); Creates double dimensional array at arr[0] So its arr[0][0] and so on. $arr=range(a, z); Is one dimensional array.
Adding Items to an Array Now pears is located at 4 and tomatoes at 5.
Deleting an Array Just as you can find the length of a string— how many characters it contains—by using strlen(), you can determine the number of elements in an array by using count(): $how_many = count($array);
Accessing Array Element echo $soups[1]// Problematic. echo "The first item is $array[0]"; echo "<p>Monday's soup is $soups['Monday']. </p>"; // Problematic For that matter, because indexes are casesensitive, $soups['monday'] is meaningless because Clam Chowder was indexed at $soups['Monday']. echo "<p>Monday's soup is {$soups['Monday']}. </p>"; // Solved The fastest and easiest way to access: foreach ($array as $key => $value) {print "<p>Key is $key. Value is $value</p>"; } Practical Script 7. 3. . .
Creating Multidimensional Array ;
$customer_record = array ( , array('Pete' , 'Smith' , '123 Main Street' , 'Atlanta', 'GA', 30001) array('Sally' , 'Parisi' , '101 South Street' , 'Atlanta' , 'GA' , 30001), array(‘Ahmad' , ‘Erbil' , '101 South Street' , ‘Erbil' , 'GA' , 44001) );
<? php ? > </body> </html>
Practice Create a php file, which has a double dimensional array as the one below. Volvo , 22 , BMW , 15 , Saab , 5 , Land Rover , 17 , 18 13 2 15 Here is the output Row number 0 Volvo 22 18 Row number 2 Saab 5 2 Row number 1 BMW 15 13 Row number 3 Land Rover 17 15
Html Array <!DOCTYPE html> <html lan='en'> <head> <title>Customer Information Form</title> </head> <body> <form method='post' action=handler. php'> <h 2> Please enter information in all fields</h 2> First Name <input type='text' name='customer_record[]' id='customer_record[]' /> Last Name <input type='text' name='customer_record[]' id='customer_record[]' /> Address City State Zip code <input type='text' name='customer_record[]' id='customer_record[]' /> <input type='number' min='11111' max='99999' name='customer_record[]' id='customer_record[]' /> <input type='submit' value="Click to submit your information" /> </form> </body> </html>
Html Array Cont. . <? php if (!isset($_POST['submit'])) { echo "submit not pressed"; } else{ $info. Array=$_POST['customer_record']; print_r($info. Array); } ? > What do you think the output is !?
Sorting Arrays
Transforming Between Strings and Arrays You know both strings and arrays, Next, switching between the formats. The first, implode(), turns an array into a string. The second, explode(), does just the opposite. Here are some reasons to use these functions: ■ To turn an array into a string in order to pass that value appended to a URL ■ To turn an array into a string in order to store that information in a database ■ To turn a string into an array to convert a comma-delimited text field (say, a keyword search area of a form) into its separate parts
$arr = array('Hello', 'World!', 'Beautiful', 'Day!'); echo implode(" ", $arr); $str = "Hello world. It's a beautiful day. "; print_r (explode(" ", $str));
https: //www. w 3 schools. com/php_ref_array. asp
Creating an Array from a Form
Questions and Open Discussion - What did you learn, you didn’t know before? Questions still remain in your mind? Best and Worst part of the presentation? What do you suggest we do most in theoretical part?
- Slides: 31