Some TIPS1) SORT & nbsp; & lt ;? Php $ websitearray ("labnol", "sml", "techiemania", "softwarebuzzer", "techperk"); sort ($ website); sortalphabeticallybyvaluepr 4 TIPS of the PHP array
Recently, some TIPS of the array is small.
1) SORT
$ Website = array ("labnol", "sml", "techiemania", "softwarebuzzer", "techperk ");
Sort ($ website );
// Sort alphabetically by value
Print_r ($ website );
?>
SORT sorts the array and the output is:
Array ([0] => "labnol" [1] => "sml" [2] => "softwarebuzzer" [3] => "techiemania" [4] =>" techperk ")
Its variant asort:
$ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Asort ($ my_array );
Print_r ($ my_array );
?>
Sort by value output:
$ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Asort ($ my_array );
Print_r ($ my_array );
?>
Ksort:
The ksort () function sorts the array by key name and retains the original key for the array value.
The second optional parameter contains an additional sorting flag.
If yes, TRUE is returned. otherwise, FALSE is returned.
$ My_array = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Ksort ($ my_array );
Print_r ($ my_array );
?>
Output:
Array
(
[A] => Dog
[B] => Cat
[C] => Horse
)
2) Array_reverse (): inverted array
$ Websites = array ("devlup", "techiemania", "shoutmeloud", "labnol ");
Print_r (array_reverse ($ websites ));
?>
Output:
("Labnol", "shoutmeloud", "techiemania", "devlup ")
3) The explode and implode are not mentioned much. they are simple and traditional.
4) array_slice ()
The function extracts a value from the array based on the condition and returns it.
$ A = array (0 => "Dog", 1 => "Cat", 2 => "Horse", 3 => "Bird ");
Print_r (array_slice ($ a, 1, 2 ));
?>
Array is required. Specifies the input array.
Offset is required. Value. Specifies the start position of the retrieved element.
If it is a positive number, it is taken from the beginning to the end. if it is a negative value, it is taken from the back to the absolute value of the offset.
Length is optional. Value. Specifies the length of the returned array.
If it is a negative number, select the element of the absolute value from the back to the front. If this value is not set, all elements are returned.
Preserve is optional. Possible values:
True-reserved key
False-default-reset key
Output:
Array ([0] => Cat [1] => Horse)