21. array_diff_key (PHP 5 >= 5.1.0)
array_diff_key — Computes the difference of arrays using keys for comparison
Description
array array_diff_key ( array $array1, array $array2 [, array $…] )
Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

$array1 = array(‘blue’ => 1, ‘red’ => 2, ‘green’ => 3, ‘purple’ => 4);
$array2 = array(‘green’ => 5, ‘blue’ => 6, ‘yellow’ => 7, ‘cyan’ => 8);
var_dump(array_diff_key($array1, $array2));


22. array_diff_uassoc (PHP 5)
array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function
Description
array array_diff_uassoc ( array $array1, array $array2 [, array $…, callback $key_compare_func] )
Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are used in the comparision.
Unlike array_diff_assoc() an user supplied callback function is used for the indices comparision, not internal function.
function key_compare_func($a, $b)
{
if ($a === $b) {
return 0;
}
return ($a > $b)? 1:-1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "key_compare_func");
print_r($result);

The above example will output:
Array( [b] => brown [c] => blue [0] => red )


23. array_pad (PHP 4, PHP 5)
array_pad — Pad array to the specified length with a value
Description
array array_pad ( array $input, int $pad_size, mixed $pad_value )
array_pad() returns a copy of the input padded to size specified by pad_size with value pad_value. If pad_size is positive then the array is padded on the right, if it’s negative then on the left. If the absolute value of pad_size is less than or equal to the length of the input then no padding takes place. It is possible to add most 1048576 elements at a time.
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
// result is array(12, 10, 9, 0, 0)
$result = array_pad($input, -7, -1);
// result is array(-1, -1, -1, -1, 12, 10, 9)
$result = array_pad($input, 2, "noop");
// not padded


24. array_pop (PHP 4, PHP 5)
array_pop — Pop the element off the end of array
Description
mixed array_pop ( array &$array )
array_pop() pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned.
Note: This function will reset() the array pointer after use.
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);


25. array_push (PHP 4, PHP 5)
array_push — Push one or more elements onto the end of array
Description
int array_push ( array &$array, mixed $var [, mixed $…] )
array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as:
$array[] = $var;

repeated for each var.
Returns the new number of elements in the array.
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);



26. array_rand (PHP 4, PHP 5)
array_rand — Pick one or more random entries out of an array
Description
mixed array_rand ( array $input [, int $num_req] )
array_rand() is rather useful when you want to pick one or more random entries out of an array. It takes an input array and an optional argument num_req which specifies how many entries you want to pick – if not specified, it defaults to 1.
If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.
Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
echo $input[$rand_keys[0]] . "n";
echo $input[$rand_keys[1]] . "n";



27. array_reduce (PHP 4 >= 4.0.5, PHP 5)
array_reduce — Iteratively reduce the array to a single value using a callback function
Description
mixed array_reduce ( array $input, callback $function [, int $initial] )
array_reduce() applies iteratively the function function to the elements of the array input, so as to reduce the array to a single value. If the optional initial is available, it will be used at the beginning of the process, or as a final result in case the array is empty. If the array is empty and initial is not passed, array_reduce() returns NULL.
function rsum($v, $w) {
$v += $w;
return $v; }

function rmul($v, $w) {
$v *= $w;
return $v; }

$a = array(1, 2, 3, 4, 5);
$x = array();
$b = array_reduce($a, "rsum");
$c = array_reduce($a, "rmul", 10);
$d = array_reduce($x, "rsum", 1);

This will result in $b containing 15, $c containing 1200 (= 10*1*2*3*4*5), and $d containing 1.


28. array_reverse (PHP 4, PHP 5)
array_reverse — Return an array with elements in reverse order
Description
array array_reverse ( array $array [, bool $preserve_keys] )
array_reverse() takes input array and returns a new array with the order of the elements reversed, preserving the keys if preserve_keys is TRUE.
$input = array("php", 4.0, array("green", "red"));
$result = array_reverse($input);
$result_keyed = array_reverse($input, true);
This makes both $result and $result_keyed have the same elements, but note the difference between the keys. The printout of $result and $result_keyed will be:

Array([0] => Array([0] =>green [1] => red)[1] => 4[2] => php)
Array([2] => Array([0] => green[1] => red)[1] => 4[0] => php)


29. array_search (PHP 4 >= 4.0.5, PHP 5)
array_search — Searches the array for a given value and returns the corresponding key if successful
Description
mixed array_search ( mixed $needle, array $haystack [, bool $strict] )
Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise.
Note: If needle is a string, the comparison is done in a case-sensitive manner.
Note: Prior to PHP 4.2.0, array_search() returns NULL on failure instead of FALSE.
If the optional third parameter strict is set to TRUE then the array_search() will also check the types of the needle in the haystack.
If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead.

$array = array(0 => ‘blue’, 1 => ‘red’, 2 => ‘green’, 3 => ‘red’);
$key = array_search(‘green’, $array); // $key = 2;
$key = array_search(‘red’, $array); // $key = 1;


30. array_shift (PHP 4, PHP 5)
array_shift — Shift an element off the beginning of array
Description
mixed array_shift ( array &$array )
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched. If array is empty (or is not an array), NULL will be returned.
Note: This function will reset() the array pointer after use.
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);


31. array_slice (PHP 4, PHP 5)
array_slice — Extract a slice of the array
Description
array array_slice ( array $array, int $offset [, int $length [, bool $preserve_keys]] )
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.

If length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Note that array_slice() will reorder and reset the array indices by default. Since PHP 5.0.2, you can change this behaviour by setting preserve_keys to TRUE.

$input = array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"
$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"
print_r(array_slice($input, 2, -1)); // note the differences in the array keys
print_r(array_slice($input, 2, -1, true));

The above example will output:
Array([0] => c[1] => d)
Array([2] => c[3] => d)


32. array_splice (PHP 4, PHP 5)
array_splice — Remove a portion of the array and replace it with something else
Description
array array_splice ( array &$input, int $offset [, int $length [, array $replacement]] )

array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements. Note that numeric keys in input are not preserved.
If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.

If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length.

If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Note that keys in replacement array are not preserved. If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself.
The following statements change the values of $input the same way:
Table 21. array_splice() equivalents
array_push($input, $x, $y) array_splice($input, count($input), 0, array($x, $y))
array_pop($input) array_splice($input, -1)
array_shift($input) array_splice($input, 0, 1)
array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y))
$input[$x] = $y // for arrays where key equals offset array_splice($input, $x, 1, $y)

Returns the array consisting of removed elements.
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
// $input is now array("red", "green")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
// $input is now array("red", "yellow")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
// $input is now array("red", "orange")

$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
// $input is now array("red", "green",
// "blue", "black", "maroon")

$input = array("red", "green", "blue", "yellow");
array_splice($input, 3, 0, "purple");
// $input is now array("red", "green",
// "blue", "purple", "yellow");



33. array_sum (PHP 4 >= 4.0.4, PHP 5)
array_sum — Calculate the sum of values in an array
Description
number array_sum ( array $array )
array_sum() returns the sum of values in an array as an integer or float.
$a = array(2, 4, 6, 8);
echo "sum(a) = " . array_sum($a) . "n";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);
echo "sum(b) = " . array_sum($b) . "n";

The above example will output:
sum(a) = 20
sum(b) = 6.9


34. array_unshift (PHP 4, PHP 5)
array_unshift — Prepend one or more elements to the beginning of an array
Description
int array_unshift ( array &$array, mixed $var [, mixed $…] )
array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won’t be touched.
Returns the new number of elements in the array.
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
The above example will output:
Array([0] => apple[1] => raspberry[2] => orange[3] => banana)


35. trim (PHP 4, PHP 5)
trim — Strip whitespace (or other characters) from the beginning and end of a string
Description
string trim ( string $str [, string $charlist] )
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
• " " (ASCII 32 (0x20)), an ordinary space.
• "t" (ASCII 9 (0x09)), a tab.
• "n" (ASCII 10 (0x0A)), a new line (line feed).
• "r" (ASCII 13 (0x0D)), a carriage return.
• "�" (ASCII 0 (0x00)), the NUL-byte.
• "x0B" (ASCII 11 (0x0B)), a vertical tab.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);
print "n";
$trimmed = trim($text);
var_dump($trimmed);
$trimmed = trim($text, " t.");
var_dump($trimmed);
$trimmed = trim($hello, "Hdle");
var_dump($trimmed);
// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
$clean = trim($binary, "x00..x1F");
var_dump($clean);

The above example will output:

string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(28) "These are a few words 🙂 …"
string(24) "These are a few words :)"
string(5) "o Wor"
string(14) "Example string"


36. ltrim (PHP 4, PHP 5)
ltrim — Strip whitespace (or other characters) from the beginning of a string
Description
string ltrim ( string $str [, string $charlist] )
Strip whitespace (or other characters) from the beginning of a string.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);

print "n";

$trimmed = ltrim($text);
var_dump($trimmed);

$trimmed = ltrim($text, " t.");
var_dump($trimmed);

$trimmed = ltrim($hello, "Hdle");
var_dump($trimmed);

// trim the ASCII control characters at the beginning of $binary
// (from 0 to 31 inclusive)
$clean = ltrim($binary, "x00..x1F");
var_dump($clean);

The above example will output:
string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(30) "These are a few words 🙂 … "
string(30) "These are a few words 🙂 … "
string(7) "o World"
string(15) "Example string
"


37. rtrim (PHP 4, PHP 5)
rtrim — Strip whitespace (or other characters) from the end of a string
Description
string rtrim ( string $str [, string $charlist] )
This function returns a string with whitespace stripped from the end of str.
$text = "ttThese are a few words 🙂 … ";
$binary = "x09Example stringx0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);

print "n";

$trimmed = rtrim($text);
var_dump($trimmed);
$trimmed = rtrim($text, " t.");
var_dump($trimmed);
$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);
// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
$clean = rtrim($binary, "x00..x1F");
var_dump($clean);

The above example will output:

string(32) " These are a few words 🙂 … "
string(16) " Example string
"
string(11) "Hello World"

string(30) " These are a few words 🙂 …"
string(26) " These are a few words :)"
string(9) "Hello Wor"
string(15) " Example string"



38. chop (PHP 4, PHP 5)
chop — Alias of rtrim()
Description
This function is an alias of: rtrim().
Notes
Note: chop() is different than the Perl chop() function, which removes the last character in the string.

39. chown (PHP 4, PHP 5)
chown — Changes file owner
Description
bool chown ( string $filename, mixed $user )
Attempts to change the owner of the file filename to user user. Only the superuser may change the owner of a file.
Notes
Note: This function will not work on remote files as the file to be examined must be accessible via the servers filesystem.
Note: When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed.


40. chr (PHP 4, PHP 5)
chr — Return a specific character
Description
string chr ( int $ascii )
Returns a one-character string containing the character specified by ascii.
This function complements ord().
$str = "The string ends in escape: ";
$str .= chr(27); /* add an escape character at the end of $str */
/* Often this is more useful */
$str = sprintf("The string ends in escape: %c", 27);

Leave a comment