What are the common number of build/value manipulation functions in PHP and what are their usage? Code

Source: Internet
Author: User
PHP array functions are a very important part of PHP, and the PHP array is made up of keys and key values, which access the values of the corresponding keys through the keys of the elements. The next step is to introduce the common number of build/value operation functions in PHP.

Associative array "refers to an array whose key name is a string, and the index and key name are the same in the associative array. The index can be understood as a numerical subscript for an array. You can use the array handler function to manipulate the keys and values of each element in a set to generate a new array.

1. PHP Array function array_values ()

The function array_values () returns the value of all the elements in the array. It is simple to use, just set a required parameter, specify the incoming array, and return an array of all the values in a given array. Without preserving the key name, the returned array will be re-indexed using sequential values, incrementing from 0 to 1. Applies to arrays with array subscript clutter, or you can convert associative arrays into indexed arrays. is a simple example and a running result.

<?php$contact = Array ("ID" = 1, "name" = "xxx", "Company" = "PHP Chinese web", "address" = "H City", "telephone" = "123456"); The Array_values () function passes in the array $contact re-index returns a new array Print_r (Array_values ($contact));p Rint_r ($contact); Original array $contact content element invariant?>

Operation Result:

Array ([0]=>1 [1]=> xxx [2]=>php Chinese] [3]=>h City [4]=>123456
Array ([id]=>1 [name]=> xxx] [Company]=>php Chinese NET [address]=> H City [Tel]=>123456

2. PHP Array function Array_keys ()

The function of Array_keys () is to return all the key names in the array. The function has a required parameter and two optional parameters, as shown in, $test is a required parameter, and if only the required parameters, all the key names of the array are returned. Specifies that the second parameter (Zhang San) returns the key name for the value. The Second optional parameter is to perform a congruent comparison. You can use the strict parameter, and if the strict parameter is specified as true, PHP uses the congruent comparison (= = =) to strictly check the data type of the key value.

3. PHP Array function In_array ()

The function of In_array () is to check if a value exists in the array and search for the given value in the array. The function has three parameters, the first two are required, and the last one is optional.

The first parameter ("Zhang San") specifies the value to search for in the array, the second parameter ("$people") is the array to be searched, if the given value ("Zhang San") exists in the array ("$ $people") returns True if the third argument is set to Ture, the function is only given a value (" Zhang San ") returns True if an array (" $people ") is present, otherwise false is returned. (the "Zhang San") parameter is a string, and the third argument is set to True, the search is case-sensitive.

4. PHP Array function Array_filp ()

The function of Array_filp () is to swap the keys and values in the array. Returns an array that, if a value appears more than once, the last key name as its value, overriding the preceding element. If the data type of the value in the original array is not a string or an integer function will error. The function has only one parameter, which is a simple example:

<?php    $lamp = Array ("OS" = "Linux", "WebServer" = "Apache", "Database" = "MySQL", "Language" and "PHP" );     Output: Array ([Linux]=>os [Apache]=>webserver] [mysql]=>database [php]=language];    Print_r (Array_flip ($lamp));             Using the Array_flip () function to swap the keys and values in an array     //In an array if the value of the element is the same, the use of array_flip () conflicts    $trans = Array ("A" =>1, "B" =>1, "C" =2 );    Print_r (Array_flip ($trans));            Now $trans becomes: Array ([1]=> b [2]=> c)?>

5. PHP Array function array_reverse ()

The function Array_reverse () Reverses the order of the elements in the original array, creating a new array to return. There are two parameters, the first is a required parameter, the second is optional, and if true, the key name of the element remains unchanged, otherwise the key name is lost.

<?php    $lamp = Array ("OS" = "Linux", "WebServer" = "Apache", "Database" = "MySQL", "Language" and "PHP" );     Use the Array_reverse () function to flip the order of the elements in the array $lamp    print_r (Array_reverse ($lamp));    The result of the output array ([language]=>php [Database]=>mysql] [Webserver]=>apache [  Os]=>linux]?>

The above is the usual array key/value operation function, first introduced to this, are the basic knowledge.

Related Courses recommended:

Common functions for PHP arrays

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.