PHP 10 join array tips

Source: Internet
Author: User
Tags spl
Welcome to the Linux community forum and interact with 2 million technical staff. 7. random array sorting involves another random sorting technology in the flash card program. In this case, you need to use shuffle () function to achieve random sorting of array items. $ Capitalsarray (ArizonaPhoenix, AlaskaJuneau, Alabama

Welcome to the Linux community forum and interact with 2 million technical staff> go to 7. random array sorting involves another random sorting technology in the flash card program. Then you need to use shuffle () function to achieve random sorting of array items. $ Capitals = array ('arizona '= 'phoenix', 'alaska '= 'juneau', 'alabama

Welcome to the Linux community forum and interact with 2 million technicians>

7. random array sorting

Another random sorting technique is involved in the flash card program. In this case, you need to use the shuffle () function to implement random sorting of array projects.

$ Capitals = array (

'Arizona '=> 'phoenix ',

'Apache' => 'juneau ',

'Babama' => 'mongomery'

);

Shuffle ($ capitals );

If you do not need to disrupt the array order and want to randomly select a value, use the array_rand () function.

8. Check whether the key and value exist.

You can use the in_array () function to determine whether an array element exists.

$ Capitals = array (

'Arizona '=> 'phoenix ',

'Apache' => 'juneau ',

'Babama' => 'mongomery'

);

If (in_array ("Juneau", $ capitals ))

{

Echo "Exists! ";

} Else {

Echo "Does not exist! ";

}

Few people know that this function can also determine whether an array key exists. At this point, it functions the same as the array_key_exists () function.

$ Capitals = array (

'Arizona '=> 'phoenix ',

'Apache' => 'juneau ',

'Babama' => 'mongomery'

);

If (array_key_exists ("Alaska", $ capitals ))

{

Echo "Key exists! ";

} Else {

Echo "Key does not exist! ";

}

9. Search for Arrays

You may want to search for Array resources, so that you can easily search associated States using a specific state government. You can use the array_search () function to search for arrays.

$ Capitals = array (

'Arizona '=> 'phoenix ',

'Apache' => 'juneau ',

'Babama' => 'mongomery'

);

$ State = array_search ('juneau ', $ capitals );

// $ State = 'alaska'

10. Standard PHP Library

The Standard PHP Library (SPL) provides developers with many data structures, iterators, interfaces, exceptions, and other functions not available in the previous PHP language, these functions can be used to traverse arrays through object-oriented syntax.

$ Capitals = array (

'Arizona '=> 'phoenix ',

'Apache' => 'juneau ',

'Babama' => 'mongomery'

);

$ ArrayObject = new ArrayObject ($ capitals );

Foreach ($ arrayObject as $ state => $ capital)

{

Printf ("The capital of % s is % s
", $ State, $ capital );

}

// The capital of Arizona is Phoenix

// The capital of Alaska is Juneau

// The capital of Alabama is Montgomery

This is only one of the many great functions of SPL. You must read the PHP manual to learn more.

[1] [2]

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.