15
ARRAY IN PHP 1 www.apextgi.in

Introduction to Array Functions in PHP

  • Upload
    apextgi

  • View
    41

  • Download
    5

Embed Size (px)

DESCRIPTION

Array is a memory allocation which is used to store or contain multiple elements with unique indexes or addresses at a continuous memory location. www.apextgi.in

Citation preview

ARRAY IN PHP

1www.apextgi.in

What is an ARRAY?

Array is a memory allocation which is used to store or contain multiple elements with unique indexes

or addresses at a continuous memory location.

With any other programming language references array is a collection of similar data types but here

in PHP we can store any data type in a single array. Because we do not need to declare data types

before the array initialization.Eg:-

2www.apextgi.in

#Initilizationof an Array

$city = array(‘delhi’, ‘shimla’, ‘agra’,noida’, ‘patna’);

Index-> 0 1 2 3 4

echo $city[0]; // output will bedelhi

3www.apextgi.in

Types of Array

1. Single or One Dimensional or Default or Numeric or Index Base Array

2. Associative Array

3. Multi Dimensional Array

4www.apextgi.in

#Predefined Constants of an Array

CASE_LOWER (integer):-Itis used with array_change_key_case() and is used to convert array keys to lower case. This is also the default case forarray_change_key_case().

CASE_UPPER (integer):- Itis used with array_change_key_case() and is used to convert array keys to upper case.

SORT_ASC (integer):-SORT_ASC is used with array_multisort() to sort in ascending order.

SORT_DESC (integer):-SORT_DESC is used with array_multisort() to sort in descending order.

5www.apextgi.in

SORT_REGULAR (integer)SORT_REGULAR is used to compare items normally.

SORT_NUMERIC (integer)SORT_NUMERIC is used to compare items numerically.

SORT_STRING (integer)SORT_STRING is used to compare items as strings.

SORT_LOCALE_STRING (integer)SORT_LOCALE_STRING is used to compare items as strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2.

SORT_NATURAL (integer)SORT_NATURAL is used to compare items as strings using "natural ordering" like natsort(). Added in PHP 5.4.0.

6www.apextgi.in

SORT_FLAG_CASE (integer)SORT_FLAG_CASE can be combined (bitwiseOR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively. Added in PHP 5.4.0.

COUNT_NORMAL (integer)

COUNT_RECURSIVE (integer)

EXTR_OVERWRITE (integer)

EXTR_SKIP (integer)

EXTR_PREFIX_SAME (integer)

7www.apextgi.in

Array Functions

array_change_key_case — Changes the case of all keys in an array

array_chunk — Split an array into chunks

array column — Return the values from a single column in the input array

array combine — Creates an array by using one array for keys and another for its valuesarray_count_values — Counts all the values of an array

array_diff_assoc — Computes the difference of arrays with additional index checkarray_diff_key — Computes the difference of arrays using keys for comparison

9www.apextgi.in

array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function

array_diff_ukey — Computes the difference of arrays using a callback function on the keys for comparison

array_diff — Computes the difference of arrays

array_fill_keys — Fill an array with values, specifying keys

array_fill — Fill an array with values

array_filter — Filters elements of an array using a callback function

array_flip — Exchanges all keys with their associated values in an array

10www.apextgi.in

array_intersect_assoc — Computes the intersection of arrays with additional index check

array_intersect_key — Computes the intersection of arrays using keys for comparison

array_intersect_uassoc — Computes the intersection of arrays with additional index check, compares indexes by a callback function

array_intersect_ukey — Computes the intersection of arrays using a callback function on the keys for comparison

array_intersect — Computes the intersection of arrays

array_key_exists — Checks if the given key or index exists in the array

array_keys — Return all the keys or a subset of the keys of an array

array_map — Applies the callback to the elements of the given arrays

11www.apextgi.in

array_merge — Merge one or more arrays

array_multisort — Sort multiple or multi-dimensional arrays

array_pad — Pad array to the specified length with a value

array_pop — Pop the element off the end of array

array_product — Calculate the product of values in an array

array_push — Push one or more elements onto the end of array

array_rand — Pick one or more random entries out of an array

array_reduce — Iteratively reduce the array to a single value using a callback function

array_replace_recursive — Replaces elements from passed arrays into the first array recursively

array_replace — Replaces elements from passed arrays into the first array

12www.apextgi.in

array_reverse — Return an array with elements in reverse orderarray_search — Searches the array for a given value and returns the corresponding key if successfularray_shift — Shift an element off the beginning of arrayarray_slice — Extract a slice of the arrayarray_splice — Remove a portion of the array and replace it with something elsearray_sum — Calculate the sum of values in an arrayarray_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback functionarray_udiff_uassoc — Computes the difference of arrays with additional index check, compares data and indexes by a callback functionarray_udiff — Computes the difference of arrays by using a callback function for data comparison

13www.apextgi.in

array_unique — Removes duplicate values from an arrayarray_unshift —Prependone or more elements to the beginning of an arrayarray_values — Return all the values of an arrayarray_walk_recursive — Apply a user function recursively to every member of an arrayarray_walk — Apply a user function to every member of an arrayarray — Create an arrayarsort — Sort an array in reverse order and maintain index associationasort — Sort an array and maintain index associationcompact — Create array containing variables and their valuescount — Count all elements in an array, or something in an objectcurrent — Return the current element in an arrayeach — Return the current key and value pair from an array and advance the array cursor

14www.apextgi.in

Thanks

facebook.com/apex.tgi

twitter.com/ApextgiNoida

pinterest.com/apextgi

Stay Connected with us for more chapters on PHP