natsort()

This function Sort an array using a “natural order” algorithm.

Syntax: natsort(array)

Description:

The argument “array” is Required. It specifies the array to sort.
The natsort() function sorts an array by using a “natural order” algorithm. The values keep their original keys.

This function implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a “natural ordering”.

Returns TRUE on success or FALSE on failure.

Example:
Code:

$array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png");

sort($array1);
echo "Standard sorting\n";
print_r($array1);

natsort($array2);
echo   "\nNatural order sorting\n";
print_r($array2);

Output:

Standard sorting Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png )
Natural order sorting Array ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png )

compact()

This function Create array containing variables and their values.

Syntax: compact(var1,var2…)

Description:

The first argument “var1” is Required. It Can be a string with the variable name, or an array of variables.
The second argument “var2” is Optional. It Can be a string with the variable name, or an array of variables. Multiple parameters are allowed.
Any strings that are not set will simply be skipped.

Example:
Code:

$city  = "Siwan";
$state = "Bihar";
$event = "marriage_ceremony";

$location_vars = array("city", "state");

$result = compact("event", "nothing_here", $location_vars);
print_r($result);

Output:

Array ( [event] => marriage_ceremony [city] => Siwan [state] => Bihar )

reset()

This function Set the internal pointer of an array to its first element.

Syntax: reset(array)

Description:

The argument “array” is Required. It specifies the array to use.
This function returns the value of the first element in the array on success, or FALSE on failure.

Example:
Code:


$course = array('MCA', 'MBA',  'M.Sc', 'MSW');  
echo current($course) . "
\n"; next($course); next($course); echo current($course) . "
\n"; reset($course); echo current($course) . "
\n";

Output:

MCA
M.Sc
MCA

array_unshift()

This function inserts new elements to an array. The new array values will be inserted in the beginning of the array.

Syntax: array_unshift(array,value1,value2,value3…)

Description:

The first argument “array” isRequired. It specifies an array.
The second argument “value1” is Required. It specifies a value to insert.
The third argument “value2” is Optional. It specifies a value to insert
The fourth argument “value3” is Optional. It specifies a value to insert.

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.

Example:
Code:

$queue = array("Black Berry", "Guavas");
 array_unshift($queue, "Mango", "Grapes");
print_r($queue);

Output:

Array ( [0] => Mango [1] => Grapes [2] => Black Berry [3] => Guavas )

array_unique()

This function Removes duplicate values from an array
Syntax: array_unique ( array )

Description:

The argument “array” is Required.It specifies an array.
array_unique() takes input array and returns a new array without duplicate values.
If two or more array values are the same, the first appearance will be kept and the other will be removed.

Example:
Code:

$veg_name  = array("a" => "Cabbage", "Carrot", "b" => "Bitter Gourd", "Cabbage", "Beans","Carrot");
$result = array_unique($veg_name);
print_r($result);

Output:

Array ( [a] => Cabbage [0] => Carrot [b] => Bitter Gourd [2] => Beans )

Page 1 of 1112345...10...Last »
eXTReMe Tracker