22
Arrays By Shyam Gurram

Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Embed Size (px)

Citation preview

Page 1: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

ArraysBy

Shyam Gurram

Page 2: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

What is an Array?

• An array can store one or more values in a single variable name.

• Each element in the array is assigned its own ID so that it can be easily accessed.

• $array[key] = value;

• There are 3 kinds of arrays

1.Numeric Array.

2. Associative Array.

3. Multidimensional Array.

Page 3: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Numeric Array

Example:

<?php

$names[0] = "Peter";

$names[1] = "Quagmire";

$names[2] = "Joe";

echo $names[1] . " and " . $names[2] .

" are ". $names[0] . "'s neighbors";

?>

• A numeric array stores each element with a numeric ID key.

$names[0] = "Peter";

$names[1] = "Quagmire";

$names[2] = "Joe";

Page 4: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Associative Array

<?php

$ages[‘Brent’] = ”42";

$ages[‘Andrew’] = ”25";

$ages[‘Joshua’] = ”16";

echo Brent is " . $ages[‘Brent’] . " years old.";

?>

• An associative array, each ID key is associated with a value.

• When storing data about specific named values, a numerical array is not always the best way to do it.

• With associative arrays we can use the values as keys and assign values to them.

Page 5: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Multidimensional Array

$families = array

(

"Griffin"=>array

(

"Peter",

"Lois",

"Megan" ),

"Quagmire"=>array ( "Glenn" ),

"Brown"=>array

(

"Cleveland",

"Loretta",

"Junior"

)

);

• In a multidimensional array, each element in the main array can also be an array.

• And each element in the sub-array can be an array, and so on.

Page 6: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Defining Arrays

• Array in PHP may be defined by two types of patterns, defining and assigning one element at a time.

• The other one is declaring, which includes a definition and initial assignment of one to many elements.

• Print_r(): This function does two things it prints the content of PHP arrays as name and value pairs;

• Boolean print_r(mixed var [, Boolean mode])

Page 7: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Defining Arrays

• <?php

$a = “dr.”; //declaring string variables

$b = “mr.”;

$c = “mrs.”;

$salutation = array(); // define an array with zero numbers.

$salutation[0] = $a; // Assigning elements using index

$salutation[0] = $b;

$salutation[0] = $c;

Print_r($salutation); ?>

Page 8: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Managing Arrays

• PHP arrays have a number of predefined functions that enable you to manage arrays. There are four types of functions1. Identification functions, such as count() and is_array()

2. Seeding and padding arrays with range() and array_pad() functions.

3. Queue Management funcitons.

4. Traversing functions

Page 9: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Identification Functions

• Syntax of count() function as follows.Int count(mixed variable [, int mode])

• The count function enables you to count the number of elements.

• Syntax of is_array() function as follows

Boolean is_array(mixed var)

• The is_array() function to get a count of all elements in a multidimensional array.

Page 10: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Seeding and Padding Arrays

• The range() function enables you to create and fill an array of numbers or characters based on a beginning value and an ending value. The syntax of range function as follows

array range(mixed low, mixed high [, int step])

• The range() function returns an array, which may be numerically indexed with a list of numbers or characters.

Page 11: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Queue Management Functions

• Arrays are sometimes managed as first-in-first-out (FIFO), first-in-last-out (FILO), last-in-first-out(LIFO), and last-in-last-out(LILO) queues there are four predefined commands to support queues, those are discussed below.

• Array_pop(): The function removes and returns the last element from a target array, which shorten the array by 1.

• Syntax for the above function as follows

mixed array_pop(array var);

Page 12: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

• Array_push(): The function adds one or more elements at the end of a target array and returns 1 for success. Syntax for the function as follows

int array_push(array var, mixed var[, mixed…..])

• Array_shift(): The function removes and returns the first element from a target array, which shorten the array by 1. The syntax for the function as follows

mixed array_shift(array var)

Page 13: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

• Array_unshift(): The function adds an element to the front of a target array and return 1 for success. The syntax for the function is as follows

Int array_unshift ( array var, mixed var[, mixed …])

Page 14: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Traversing Functions

• There are seven predefined traversing functions all of them are explained below

• Current(): The function takes one formal parameter, which is a target array. The current() function returns the value of the current element. Syntax for the current function is as follows

mixed current(array var)

Page 15: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Traversing Functions

• Each(): The function takes one formal parameter, which is a target array, and returns an array of four values. The syntax for the each() function is as follows

array each(array var)

• End(): The function moves the pointer to the last element of the array and returns the value of the last element. The syntax is as follows

mixed end(array var)

Page 16: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Traversing Functions

• Key(): The function takes one formal parameter, which is a target array. The key() functions returns the key of current element. Syntax for the function is below

mixed key(array var)

• Next(): The function takes one formal parameter, which is a target array, and returns the next element in the array by moving the pointer forward.

mixed next(array var)

Page 17: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Traversing Functions

• Prev(): The function takes one formal parameter, which is a target array, and return the previous element in the array by moving the pointer backward.

Syntax for the above function is as follows

mixed prev(array var)

• Reset(): The function moves the pointer to the first element of the array and returns the value of the first element. Syntax is as follows

mixed reset(array var)

Page 18: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Sorting Arrays

• Sorting is very important function in arrays, there are some functions which supports sorting in PHP programming, those are explained below.

• Arsort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in reverse or descending order, preserving the native keys of the original array, and has this syntax:

void arsort(array var [, int sort_flag])

Page 19: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Sorting Arrays

• Asort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in ascending order, preserving the native keys of the original array, and has this syntax

void asort(array var [, int sort_flag])

• Array_multisort(): The function takes a list of formal parameters that can include an array, a sort order flag, and sort type flag, followed by other array values. There can only be one actual parameter for the order or type flag value. Subsequent values will not raise error but will not be recognized or processed in PHP 5.0 and 5.1. Syntax:

Boolean array_multisort(array var1 [, mixed order[, mixed type [, array var2]]..])

Page 20: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Sorting Arrays

• Natcasesort(): The function takes one formal parameter, which is an array. Itsorts in ascending order by the values and uses rules like those in the natsort() function. The function does preserve keys during sorting and differs from the natsort() in ignoring case during sort operations. It has the following Syntax:

void natcasesort(array var).

Page 21: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Sorting Arrays

• Rsort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in descending or reverse order and reassigns native keys to new sequenced numeric key lists. It has this syntax

void rsort(array var [, int sort_flag]).

• Sort(): The function takes two formal parameters: the first is an array, and the second is a sorting flag. It sorts in ascending order but reassigns the native keys to new sequenced numeric key lists. It has this Syntax

void sort(array var [, int sort_flag])

Page 22: Arrays By Shyam Gurram. What is an Array? An array can store one or more values in a single variable name. Each element in the array is assigned its own

Merging and Partitioning Arrays

• Merging arrays joins two or more arrays into a single array. Partitioning carves data from arrays, there are some functions which are used to implement the merging concepts.

• Array_chunk(), array_combine(),array_diff(),array_diff_key(), array_diff_uassoc(), array_diff_ukey(), array_filter(), array_flip(), array_intersect(), array_intersect_assoc(), array_intersect_key(), array_intersect_ukey(), array_merge(), array_splice().

• Every function is having importance.