For all web developpers and webpage layout makers, also for beginners who desire to learn! :-)

Post news Report RSS PHP reference: arrays

Hopefully, a complete list of array functions, explained practically ONLY through code and in close detail.

Posted by on

If you don't know these functions, their descriptions, you can find some info about them below:
W3schools.com

This reference list puts most of its focus on PRACTICAL, technical side of these functions. Learning by pure practice. The use itself of array functions can be the clearest explanation of what they are for. Parameters/patterns are explained first.

<?php

//LEGEND/explanations of parameters (in brackets, e.g. 
//sizeof(array, mode), here "array" and "mode" are parameters

//array: a variable containing an array or an array, e.g.:
//$a1 = array(5, 10, 15); array_pop($a1); 
//array_pop(array(5, 10, 15));

//"..." refers to optional additional arrays (as in array_diff(array1, array2, ...)), e.g.:
//array_diff($array1, $array2, $array3, $array4);
//or
//array_diff($array1, $array2, $array3, $array4, $array5);
//or
//array_diff($array1, $array2); // no additional arrays (two are required)

//key: each value can be assigned to a key, the key tells which value is which, e.g.:
//array(1 => "Cat", 2 => "Lioness");
//array("a" => "Cat", "b" => "Lioness");
//array("A" => "Cat", "B" => "Lioness");
//1, 2, a, b, and A, B are all keys.

//value: values, "items" inside an array:
//array("Cat", "Lioness", "Tigress");
//Cat, Lioness, and Tigress are all values that can be found inside an array.

//mode: detects multidimensional arrays (arrays within arrays)
//0 or 1, e.g.
//sizeof($array1, 1); // sizeof(array, mode)
//added in PHP 4.2

//sortorder:
//SORT_
//_ASC ascending
//_DESC descending

//sorttype:
//SORT_
//_REGULAR normal sorting
//_NUMERIC numerical sorting (based on numbers)
//_STRING alphabetical sorting
//_LOCALE_STRING alphabetical sorting based on local settings

//extract_rules:
//EXTR_
//_OVERWRITE on the collision (with the existing variable), the existing variable is overwritten
//_SKIP the existing variable isn't overwritten
//_PREFIX_SAME on collision the variable name is given a prefix
//_PREFIX_ALL all variable names are given a prefix (no collision required)
//_PREFIX_INVALID only invalid or numeric variables are given a prefix
//_IF_EXISTS overwrite variables in the current array, otherwise (if there aren't any) do nothing
//_REFS variables are extracted as references, imported variables/references are still linked to the values of the array parameter

//strict:
//if TRUE
//string "1" isn't the same as integer 1 (1 != "1")
//compare:
//$var1 = 1;
//$var2 = "1";
//(the same values, but different variable types)

//preserve:
//preserve existing keys (keys, e.g. 1, 2, 3, a, b, c, etc.)

//start:
//counting start number (e.g. counting starts from 0)

//length:
//number of values, from the start (start parameter) number

//function:
//function that is being used to parse an array
//use only the NAME of the function, e.g. 
//functionForArray();
//will be
//"functionForArray"

//function_parameter
//function parameter used in the given array function, e.g.
//functionForArray(functionParameter)
//will be
//"functionParameter"

//end
//the end value, which ends the increment, e.g. start is 0, end is 100, increment is 10
//0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100

//increment/decrement
//amount by which the number is de/in-cremented, e.g. 1, 5, 10, -10:
//0, 1, 2
//0, 5, 10, 15
//0, 10, 20, 30
//0, -10, -20, -30

//initial_value:
//added before parsed array (at the beginning of it)

$array1 = array("1 num ID" => "Cat"); // array(key => value)
array_change_key_case($array1, CASE_UPPER); // array_change_key_case(array, case)
array_chunk($array1, 1, true); // array_chunk(array, size, preserve_key)
array_combine($array1, $array2); // array_combine(array1, array2)
array_count_values($array2); // array_count_values(array)
array_diff($array1, $array2, $array3); // array_diff(array1, ...)
array_diff_assoc($array1, $array2); // array_diff_assoc($array1, ...)
array_diff_key($array1, $array); // array_diff_key($array1, ...)
array_diff_uassoc($array1, $array2, "Function"); // array_diff_uassoc(array1, array2, ..., function_name)
array_diff_ukey($array1, $array2, "Function"); // array_diff_ukey(array1, array2, ..., function_name)
array_fill(0, 10, "Cat"); // array_fill(start, number_of_occurences, value)
array_filter($array1, "Function"); // array_filter(array, function)
array_flip($array1); // array_flip(array)
array_intersect($array1, $array2); // array_intersect(array1, array2, ...)
array_intersect_assoc($array1, $array2); // array_intersect_assoc(array1, array2, ...)
array_intersect_key($array1, $array2); // array_intersect_key(array1, array2)
array_intersect_uassoc($array1, $array2, "Function"); // array_intersect_uassoc(array1, array2, function_name)
array_intersect_ukey($array1, $array2, "Function"); // array_intersect_ukey(array1, array2, function_name)
array_key_exists("A", $array1); // array_key_exists(key, array)
array_map("Function", $array1, $array2); // array_map(function_name, array1, ...)
array_merge($array1, $array2); // array_merge(array1, ...)
array_merge_recursive($array1, $array2); // array_merge_recursive(array1, ...)
array_multisort($array1, SORT_ASC, SORT_NUMERIC, $array2); // array_multisort(array1, sortorder, sorttype, array2, ...)
array_pad($array1, -5, "Value"); // array_pad(array, size, value)
array_pop($array1); // array_pop(array)
array_product($array1) // array_product(array)
array_push($array1, "Tigress", "Lioness"); // array_push(array, value1, value2, ...)
array_rand($array1, 3); // array_rand(array, number)
array_reduce($array1, "Function", "animals"); // array_reduce(array, function, initial_value)
array_reverse($array1, true); // array_reverse(array, preserve)
array_search("Cat", $array1, true); // array_search(value, array, strict)
array_shift($array1); // array_shift(array)
array_slice($array1, 0, 3, true); // array_slice(array, start, length, preserve)
array_splice($array1, 0, 3, $array2); // array_splice(array1, start, length, array2)
array_sum($array1); // array_sum(array)
array_udiff($array1, $array2, "Function"); // array_udiff (array1, array2, ..., function)
array_udiff_assoc($array1, $array2, "Function"); // array_udiff_assoc (array1, array2, ..., function)
array_udiff_uassoc($array1, $array2, "Function1", "Function2"); // array_udiff_uassoc($array1, $array2, ..., function1, function2)
array_uintersect($array1, $array2, "Function"); // array_udiff_uintersect($array1, $array2, ..., function)
array_uintersect_assoc($array1, $array2, "Function"); // array_udiff_uintersect_assoc ($rray1, array2, ..., function)
array_uintersect_uassoc($array1, $array2, "Function1", "Function2"); // array_uintersect_uassoc (array1, array2, function1, function2)
array_unique($array1); // array_unique(array)
array_unshift($array1, "Lioness", "Tigress"); // array_unshift(array, value1, value2, ...)
array_values($array1); // array_values(array)
array_walk($array1, "Function", "Parameter"); // array_walk(array, function, function_parameter)
array_walk_recursive($array1, "Function", "Parameter"); // array_walk_recursive($array1, function, F_parameter)
arsort($array1, SORT_REGULAR); // arsort(array, sorttype)
asort($array1, SORT_LOCALE_STRING); //asort(array, sorttype)
$lioness = "Lioness"; $array1 = compact("lioness", "tigress", "cat"); // compact(variable1, variable2, ...)
count($array1, 1); // count(array, mode)
current($array1); // current(array)
each($array1); // each(array)
end($array1); // end(array)
extract($array1, EXTR_PREFIX_SAME, "prefix"); // extract(array, extract_rules, prefix)
in_array("Lioness", $array1, true); // in_array(search, array, type)
key($array1); // key(array)
krsort($array1, SORT_NUMERIC); // krsort(array, sorttype)
ksort($array1, SORT_REGULAR); // ksort(array, sorttype)
$a = array("Lioness", "Cat"); list($l, $c) = $a; // list(variable1, variable2)
natcasesort($array1); // natcasesort(array)
natsort($array1); // natsort(array)
next($array1); // next(array)
pos($array1); current($array1); // current/pos(array)
prev($array1); // prev(array)
range(0, 50, 5); // range(start, end, increment/decrement)
reset($array1); // reset(array)
rsort($array1, SORT_LOCALE_STRING); // rsort(array, sorttype)
shuffle(array1); // shuffle(array)
sizeof($array1, 1); // sizeof(array, mode)
sort($array1, SORT_LOCALE_STRING); // sort(array, sorttype)
uasort($array1, "Function"); // uasort(array, function)
uksort($array1, "Function"); //uksort(array, function)
usort($array1, "Function"); // usort(array, function)

//parameter: CONSTANTS
//case: CASE_LOWER, CASE_UPPER
//sortorder: SORT_ASC, SORT_DESC
//sorttype: SORT_REGULAR, SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING
//extract_rules: EXTR_OVERWRITE, EXTR_SKIP, EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, EXTR_PREFIX_IF_EXISTS, EXTR_IF_EXISTS, EXTR_REFS

//parameters/CONSTANTS used in following functions:
//
//case
//array_change_key_case(array, case)
//
//sortorder
//array_multisort(array1, sortorder, sorttype, array2, ...)
//
//sorttype
//array_multisort(array1, sortorder, sorttype, array2, ...)
//arsort(array, sorttype)
//asort(array, sorttype)
//krsort(array, sorttype)
//ksort(array, sorttype)
//rsort(array, sorttype)
//sort(array, sorttype)
//
//extract_rules
//extract(array, extract_rules, prefix)
//

?>
Post comment Comments
DDrago
DDrago - - 45 comments

thanks you man, it has been very useful :)

Reply Good karma Bad karma+2 votes
Guest
Guest - - 688,627 comments

This comment is currently awaiting admin approval, join now to view.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: