PHP related array usage tips _ PHP Tutorial

Source: Internet
Author: User
Tags spl
PHP related array usage tips. During PHP development, you need to create many similar variables early or late. at this time, you can store data as elements in the array. Elements in the array are either in PHP development or early or late. you need to create many similar variables. in this case, you can store data as elements in the array. All elements in the array have their own IDs, so they can be easily accessed.

Join array

Join array. each ID key is associated with a value. It is not the best practice to use a numeric array to store data related to specific named values. By associating arrays, we can use values as keys and assign values to them.

Here we will introduce 10 PHP-related array operations and use them to help you improve development efficiency.

1. add array elements

PHP is a weak type language, which means you do not need to declare an array and its size. On the contrary, you can declare and fill the array at the same time.

$capitals = array(    'Alabama' => 'Montgomery',    'Alaska'  => 'Juneau',    'Arizona' => 'Phoenix'  ); 

The additional array elements can be appended as follows:

$capitals['Arkansas'] = 'Little Rock'; 

If you are processing a numeric index array, you may want to use the frontend and append elements of the display naming function, such as the array_push () and array_unshift () functions, but these functions cannot operate the associated array.

2. delete array elements

To delete an element from an array, use the unset () function, for example:

unset($capitals['California']); 

When using a numeric index array, there are more ways to delete array elements. you can use the array_shift () and array_pop () functions to delete an element from the beginning and end of the array.

3. exchange keys and values

Suppose you want to create a new array named $ states, use the state government as the index, and use the state name as the associated value. The array_flip () function can easily complete this task.

$capitals = array(    'Alabama' => 'Montgomery',    'Alaska'  => 'Juneau',    'Arizona' => 'Phoenix'  );  $states = array_flip($capitals);  // $states = array(  //  'Montgomery' => string 'Alabama',  //  'Juneau'     => string 'Alaska',  //  'Phoenix'    => string 'Arizona'  // ); 
4. Merge arrays

If the preceding array is used by a Web-based "FlashCard" service, you can use array_merge () to test students' knowledge about the state capitals in the United States () functions merge arrays containing states and capitals.

$stateCapitals = array(    'Alabama' => 'Montgomery',    'Alaska'  => 'Juneau',    'Arizona' => 'Phoenix'  );  $countryCapitals = array (    'Australia' => 'Canberra',    'Austria'   => 'Vienna',    'Algeria'   => 'Algiers'  );  $capitals = array_merge($stateCapitals, $countryCapitals); 
5. edit the array value

If the data in the array contains case-insensitive errors, you can use the array_map () function to apply a callback to each array element before inserting the data into the database.

function capitalize($element)  {    $element = strtolower($element);    return ucwords($element);  }  $capitals = array(    'Alabama' => 'montGoMEry',    'Alaska'  => 'Juneau',    'Arizona' => 'phoeniX'  );  $capitals = array_map("capitalize", $capitals); 
6. sort arrays by pressing the buttons

The flash card program uses various sorting methods. for example, you can use the ksort () function to sort the associated arrays in alphabetical order.

$capitals = array(    'Arizona' => 'Phoenix',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  ksort($capitals); 

Because the array is passed to The ksort () function through parameters, it means that you no longer need to assign the sorting result to another variable.

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',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  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',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  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',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  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',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  $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',    'Alaska'  => 'Juneau',    'Alabama' => 'Montgomery'  );  $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 documentation for more information.

During development in PHP, you need to create many similar variables early or late. at this time, you can store data as elements in the array. All elements in the array have...

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.