Four TIPS of the PHP array

Source: Internet
Author: User
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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.