Slide môn học PHP session 5 working with arrays

28 173 0
Slide môn học PHP session 5 working with arrays

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Working with Arrays Session 13 Review    Function is block of code that has performs a specified job Functions enhance the logical flow in a program by dividing up complicated code sequences into smaller modules There are several functions:     Mathematical String Date and Time Functions Error Handling Functions PHP / Session 13 / Slide of 28 Objectives  Define an array  Create and use array  Merge array  Use single and multidimensional arrays  Use array-related functions PHP / Session 13 / Slide of 28 Define an array      An array is a variable that can store a set of values of the same data type Each value in an array is termed as an element Each element of an array can be referred by an array name and an index An array index is used to access an element An index can be a number or a string PHP / Session 13 / Slide of 28 Array array_name[key]=“value”;   Key is referred as index An index can be     If an If an Number String the index is a string, the array is associative array the index is a number, the array is indexed array PHP / Session 13 / Slide of 28 array() Function - I    Create an array and set the initial values of array elements Uses key value pairs separated by a comma to create an array Can initialize both indexed and associative arrays PHP / Session 13 / Slide of 28 array() Function - II Syntax for creating an array using the array() function is: $array_name = array([key => ] value, [key => ] value) PHP / Session 13 / Slide of 28 Array Identifier   Initialize value of a specific element in an array Syntax for creating an array using the array identifier is: $array_name[key] = “element_value”; PHP / Session 13 / Slide of 28 Indexed Array    An array where the index type is integer By default, PHP creates an indexed array, if the index type is not specified at the time of creating an array Index value can start with any integer, such as 1, 20, or 123 PHP / Session 13 / Slide of 28 Indexed Array - Example $department = array( => ‘Finance’, => ‘Sales’, => ‘HR’, => ‘Purchase’) echo $department[1] displays: Finance PHP / Session 13 / Slide 10 of 28 Merged Array - Example $ITdept = array( => “Testing”, => “Training”); $SalesPurcahsedept = array( => “Advertising”, => “Marketing”); $AdminDept = array_merge($ITdept, $SalesPurchasedept) PHP / Session 13 / Slide 14 of 28 Multidimensional Arrays  Each element is an array  Each element requires an array name and multiple set of indices  Syntax for creating a multidimensional array is: $array_name = array( array(key => value), array(key => value)) PHP / Session 13 / Slide 15 of 28 Multidimensional Array - Example $country_mdlist = array( “USA” => array( “Capital” => “Washington D.C.”, “Currency” => “US Dollar”), “England” => array( “Capital” => “London”, “Currency” => “Pound Sterling”), “Australia” => array( “Capital” => “Canberra”, “Currency” => “Australian Dollar”), “New Zealand” => array( “Capital” => “Wellington”, “Currency” => “NZ Dollar”)); PHP / Session 13 / Slide 16 of 28 Array-related Functions - I      sort() - Arranges the element values into an alphabetical order rsort() - Sorts the element values in the descending alphabetical order arsort() - Sorts both associative and indexed arrays count() - Indicates the number of elements in an array array_count_values() - Keeps a count of the occurrences of same element values in an array It returns the number of occurrences of same element values PHP / Session 13 / Slide 17 of 28 Array-related Functions - II     array_flip() - Converts the element values to index values and index values to element values array_intersect() - Identifies and returns the common element value among a group of arrays array_keys() - Displays all the key indices of the specified array array_reverse() - Reverses the order of the array elements PHP / Session 13 / Slide 18 of 28 sort() Function  Arranges the element values into an alphabetical order Key Value Finance Sales HR Purchase Key Value Finance Sales HR Purchase PHP / Session 13 / Slide 19 of 28 rsort() Function  Sorts the element values in the descending alphabetical order Key Value Finance Sales HR Purchase Key Value Sales Purchase HR Finance PHP / Session 13 / Slide 20 of 28 arsort() Function  Sorts both associative and indexed arrays Key Value Finance Sales HR Purchase Key Value Sales Purchase HR Finance PHP / Session 13 / Slide 21 of 28 array_flip() Function  Converts the element values to index values and index values to element values Key Value Finance Sales HR Purchase Key Finance Sales HR Purchase Value PHP / Session 13 / Slide 22 of 28 array_reverse() Function  Reverses the order of the array elements Key Value Finance Sales HR Purchase Key Value Purchase HR Sales Finance PHP / Session 13 / Slide 23 of 28 array_pop() Function  Pops and returns the last value of an array Key Value Finance Sales HR Purchase Key Value Finance Sales HR PHP / Session 13 / Slide 24 of 28 array_push() Function  Adds one or more elements to the end of an array Key Value Finance Sales HR Key Value Finance Sales HR Purchase PHP / Session 13 / Slide 25 of 28 Summary - I        A variable can store only one value at a time An array is a variable that can store a set of values of the same data type Each value in an array is termed as an element All the elements are referenced by a common name An array index is used to access an element An index can be a number or a string If the index is a string, the array is an associative array PHP / Session 13 / Slide 26 of 28 Summary - II     We can combine the element values of two or more arrays This process is called as merging arrays In a single-dimensional array, the element includes only one level of key value pairs In the multidimensional array, each element is an array Each element requires an array name and multiple set of indices There are two types of arrays    Indexed array Associative array An indexed array is an array where the index PHP / Session 13 / Slide 27 of 28 type is integer Summary - III     An associative array is an array where the index type is string In associative arrays, the index value must be specified within double quotes Multidimensional arrays are arrays that store another array within it Some of the array related functions are       sort() arsort() rsort() array_pop() array_push() array_flip() PHP / Session 13 / Slide 28 of 28 [...]... value must be specified within double quotes PHP / Session 13 / Slide 11 of 28 Associative Array - Example $department = array( “a” => ‘Finance’, “b” => ‘Sales’, “c” => ‘HR’, “d” => ‘Purchase’) echo $department[“c”] displays: HR PHP / Session 13 / Slide 12 of 28 Merging Arrays    Combine the element values of two or more arrays Use array_merge() function Syntax for merging arrays is: $merged_array_name... Finance PHP / Session 13 / Slide 23 of 28 array_pop() Function  Pops and returns the last value of an array Key 0 1 2 3 Value Finance Sales HR Purchase Key 0 1 2 Value Finance Sales HR PHP / Session 13 / Slide 24 of 28 array_push() Function  Adds one or more elements to the end of an array Key 0 1 2 Value Finance Sales HR Key 0 1 2 3 Value Finance Sales HR Purchase PHP / Session 13 / Slide 25 of 28... index PHP / Session 13 / Slide 27 of 28 type is integer Summary - III     An associative array is an array where the index type is string In associative arrays, the index value must be specified within double quotes Multidimensional arrays are arrays that store another array within it Some of the array related functions are       sort() arsort() rsort() array_pop() array_push() array_flip() PHP. .. Sorts both associative and indexed arrays Key 0 1 2 3 Value Finance Sales HR Purchase Key 1 3 2 0 Value Sales Purchase HR Finance PHP / Session 13 / Slide 21 of 28 array_flip() Function  Converts the element values to index values and index values to element values Key 0 1 2 3 Value Finance Sales HR Purchase Key Finance Sales HR Purchase Value 0 1 2 3 PHP / Session 13 / Slide 22 of 28 array_reverse()... $merged_array_name = array_merge($first_array, $second_array); PHP / Session 13 / Slide 13 of 28 Merged Array - Example $ITdept = array( 0 => “Testing”, 1 => “Training”); $SalesPurcahsedept = array( 0 => “Advertising”, 1 => “Marketing”); $AdminDept = array_merge($ITdept, $SalesPurchasedept) PHP / Session 13 / Slide 14 of 28 Multidimensional Arrays  Each element is an array  Each element requires an... / Slide 18 of 28 sort() Function  Arranges the element values into an alphabetical order Key 0 1 2 3 Value Finance Sales HR Purchase Key 0 1 2 3 Value Finance Sales HR Purchase PHP / Session 13 / Slide 19 of 28 rsort() Function  Sorts the element values in the descending alphabetical order Key 0 1 2 3 Value Finance Sales HR Purchase Key 0 1 2 3 Value Sales Purchase HR Finance PHP / Session 13 / Slide. .. associative array PHP / Session 13 / Slide 26 of 28 Summary - II     We can combine the element values of two or more arrays This process is called as merging arrays In a single-dimensional array, the element includes only one level of key value pairs In the multidimensional array, each element is an array Each element requires an array name and multiple set of indices There are two types of arrays  ... values PHP / Session 13 / Slide 17 of 28 Array-related Functions - II     array_flip() - Converts the element values to index values and index values to element values array_intersect() - Identifies and returns the common element value among a group of arrays array_keys() - Displays all the key indices of the specified array array_reverse() - Reverses the order of the array elements PHP / Session. .. an array  Each element requires an array name and multiple set of indices  Syntax for creating a multidimensional array is: $array_name = array( array(key => value), array(key => value)) PHP / Session 13 / Slide 15 of 28 Multidimensional Array - Example $country_mdlist = array( “USA” => array( “Capital” => “Washington D.C.”, “Currency” => “US Dollar”), “England” => array( “Capital” => “London”, “Currency”... Zealand” => array( “Capital” => “Wellington”, “Currency” => “NZ Dollar”)); PHP / Session 13 / Slide 16 of 28 Array-related Functions - I      sort() - Arranges the element values into an alphabetical order rsort() - Sorts the element values in the descending alphabetical order arsort() - Sorts both associative and indexed arrays count() - Indicates the number of elements in an array array_count_values()

Ngày đăng: 30/11/2016, 22:11

Mục lục

  • Working with Arrays

  • Slide 2

  • Objectives

  • Define an array

  • Array

  • array() Function - I

  • array() Function - II

  • Array Identifier

  • Indexed Array

  • Indexed Array - Example

  • Associative Array

  • Associative Array - Example

  • Merging Arrays

  • Merged Array - Example

  • Multidimensional Arrays

  • Multidimensional Array - Example

  • Array-related Functions - I

  • Array-related Functions - II

  • sort() Function

  • rsort() Function

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan