PHP Array function Knowledge Summary _php skill

Source: Internet
Author: User
Tags array example learn php learn php programming php programming

This article for everyone to share the basic knowledge of PHP array functions for your reference, the specific contents are as follows

Array arrays are very important data types. It is more like a structure than other data types, and the result structure can store a series of values. Arrays can store many values in a single variable name, and can access a value by referencing the label below.
In PHP, there are three types of arrays:
Index array-an array with a numeric index
Associative array-An array with the specified key
Multidimensional array-An array that contains one or more arrays

First, create an array

Array (key => value)

1. Create an indexed array

Use the array () function to declare an array. PHP is a weak type language relatively flexible and convenient, can also be directly an array of element values value, do not need key value keys, the index is automatically allocated (index starting from 0).
Cases:

Array ("1" => "Baidu", "2" => "Ali", "3" => "Tencent");
Or do not use the key value:
Array ("Baidu", "Ali", "Tencent");
Of course, can also be written:
$arr [0] = "Baidu";
$arr [1] = "Ali";
$arr [2] = "Tencent";

2. Creating associative arrays

Associative arrays are similar to indexed arrays, except that associative arrays cannot be as numeric as the key names of an indexed array, which can be numeric and string and mixed, and whether an array is an associative array is based on whether a key name in the array exists without a number. No, it's related.

Array ("Li" => "Baidu", "Ma" => "Ali", "Ma Teng" => "Tencent");

3, multidimensional array

Array (array (), array ()) Two-dimensional arrays

Gets the length of the array-count () function

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
echo count ($arr);
? >//Results return 3 (description of three elements in the array)

Second, traversing the array

The value of the element in the output array. Common for, foreach for indexed arrays, and foreach for associative arrays. Use the Print_r () function to print the results of a loop, or you can use Var_dump ().

1, traversing the index array

Traverses and outputs all the values of an indexed array, you can use for, foreach (array_expression as Value), foreach (Arrayexpressionaskey => $value) loops, as follows:

Use for loop

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
$arrlen = count ($arr);//Get the length of the array for
($i =0; $i < $arrlen; $i + +) { 
  $data [] = $arr [$i]; 
}
echo "<pre>"; Line break display
Print_r ($data);

The printed results appear as follows:
Array
(
[0] => Baidu
[1] => Ali
[2] => Tencent
)
Using the Foreach Loop

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
foreach ($arr as $value) {
  $data [] = $value;
}
echo "<pre>"; Line Wrap shows
print_r ($data);//print results as above

Note: After data there is an array symbol [], why?

2, traversing the associative array

Traversing and outputting all the values of the associative array, you can use the foreach (array_expression as Key=>value) loop, as follows:

<?php
$arr = Array ("Li" => "Baidu", "Ma" => "Ali", "Ma Teng" => "Tencent");
foreach ($arr as $key => $value) {
  $data [$key] = $value;
}
echo "<pre>"; Line break display
Print_r ($data);

Print results show:

Array
(
  [Robin Li] => Baidu
  [Ma Yun] => ali
  [Ma Teng] => Tencent
)

Did you notice that? The data at this time is [$key]? rather than []
An associative array of numbers is an array of numeric indices,

Add elements to delete an array

Increases the tail of an array element
The Array_push (array,value1,value2.) function adds one or more elements (into the stack) to the end of the array of the first argument, and then returns the length of the new array.
This function is equal to calling Array[]=value multiple times.

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
Array_push ($arr, "Know", "Weibo");
echo "<pre>"; Line break display
Print_r ($arr);
Print results show:
Array
(
  [0] => Baidu
  [1] => Ali
  [2] => Tencent
  [3] => know
  [4] => Weibo
)

Increases at the beginning of the array element
Array_unshift (Array,value1,value2,value3 ...) function is used to insert new elements into an array. The value of the new array is inserted at the beginning of the array.

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
Array_unshift ($arr, "Know", "Weibo");
echo "<pre>"; Line break display
Print_r ($arr);
Print results show:
Array
(
  [0] => know
  [1] => Weibo
  [2] => Baidu
  [3] => Ali
  [4] => Tencent c28/>)

Delete at the end of an array element
The Array_pop (array) function deletes the last element in the array.

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
Array_pop ($arr);
echo "<pre>"; Line break display
Print_r ($arr);
Print results display:
Array
(
  [0] => Baidu
  [1] => Ali
)

Delete at beginning of array element
The Array_shift (array) function deletes the first element in the array and can return the value of the deleted element.

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
Array_shift ($arr);
echo "<pre>"; Line break display
Print_r ($arr);
Print results show:
Array
(
  [0] => Ali
  [1] => Tencent
)

To move a duplicate value in a divisor group
The Array_unique (array) function shifts the duplicate values in the array, and returns the arrays of results.

<?php
$arr = Array ("Baidu", "Ali", "Tencent", "Baidu", "Weibo");
$data = Array_unique ($arr);
echo "<pre>";
Print_r ($data);
Print results show:
Array
(
  [0] => Baidu
  [1] => Ali
  [2] => Tencent
  [4] => Weibo
)

Iv. Locating array elements

Search for values that exist in an array
In_array (Search,array,type) checks whether the specified value exists in the array.
Returns true if the given value search exists in array arrays. If the third argument is set to True, the function returns true only if the element exists in the array and the data type is the same as the given value. If the argument is not found in the array, the function returns FALSE.

<?php
$arr = Array ("Baidu", "Ali", "Tencent");
while (In_array ("Baidu", $arr)) {
  echo "has been found";
  break;
The output has been found

To remove a value from a condition in an array: Array_slice (Array,start,length,preserve)
Start Required. Numerical. Specify the starting position of the element to be removed. 0 = first element.
If the value is set to a positive number, the previous start is taken.
If the value is set to a negative number, the start absolute value is fetched forward. -2 means starting with the penultimate element of the array.

Length is optional. Numerical. Specify the length of the array to be returned.
If the value is set to an integer, the number of elements is returned.
If the value is set to a negative number, the function terminates the removal at such a distance from the end of the array example.
If the value is not set, returns all elements starting at the start parameter setting until the end of the array.

<?php
$arr = Array ("Baidu", "Ali", "Tencent", "know", "Weibo");
$data = Array_slice ($arr, 0,4);
echo "<pre>";
Print_r ($data);
Print results show:
Array
(
  [0] => Baidu
  [1] => Ali
  [2] => Tencent
  [3] => know
)

The Array_splice (array,start,length,array) function removes the selected element from the array and replaces it with a new element. The function will also return an array that contains the removed elements.

<?php
$arr 1 = Array ("Baidu", "Ali", "Tencent");
$arr 2 = Array ("Known", "Weibo");
Array_splice ($arr 1,0,2, $arr 2);
echo "<pre>";
Print_r ($arr 1);
Print results show:
Array
(
  [0] => know
  [1] => Weibo
  [2] => Tencent
)

V. Array merging, splitting, comparison

The Array_merge () function merges the arrays together, returning a federated array. The resulting array begins with the first input array parameter, appended in the order in which the following array parameters appear.

<?php
$arr 1 = Array ("Baidu", "Ali", "Tencent");
$arr 2 = Array ("Known", "Weibo");
$data = Array_merge ($arr 1, $arr 2);
echo "<pre>";
Print_r ($data);
Print results show:
Array
(
  [0] => Baidu
  [1] => Ali
  [2] => Tencent
  [3] => know
  [4] => Weibo
)

Recursive append array
The array_merge_recursive () function is the same as Array_merge (), and you can combine two or more arrays together to form a federated array. The difference between the two is that when a key in an input array already exists in the result array, the function takes a different approach. Array_merge () overrides the previously existing key/value pair, replacing it with a key/value pair in the current input array, and array_merge_recursive () merges two values together to form a new array with the original key as the array name. In the form of:

$arr = Array (' One ' => ' C ', ' One ' => ' B '); 
$arr 1= Array (' Three ' => ' 1 ', ' One ' => ' 2 '); 
$arr 2= array_merge_recursive ($arr, $arr 1); 
echo "<pre>";
Print_r ($arr 2); 
Print results are displayed:
array
([one
  ] => array
    (
      [0] => B
      [1] => 2
    )

  [three] => 1
)

Merging two arrays
The Array_combine () function generates a new array that consists of a set of committed keys and corresponding values, in the form of:

$arr = Array (' A ', ' B '); 
$arr 1= Array (' 1 ', ' 2 '); 
$arr 2= array_combine ($arr, $arr 1);
echo "<pre>";
Print_r ($arr 2); 
Print results display:
Array
(
  [A] => 1
  [B] => 2
)

To find the intersection of an array
The Array_intersect () function returns an array of reserved keys that consist of only the values that appear in the first array and that appear in each of the other input arrays. The form is as follows:

$arr = Array (' A ', ' B ', ' C ', ' D '); 
$arr 1= Array (' A ', ' B ', ' E '); 
$arr 2= Array (' A ', ' F ', ' D '); 
$arr 3= array_intersect ($arr, $arr 1, $arr 2); 
echo "<pre>";
Print_r ($arr 3); 
Printed results are shown:
Array
(
  [0] => A
)

Note: Array_intersect () is considered equal only if two elements have the same data type.

To find the intersection of an associative array
ARRAY_INTERSECT_ASSOC () is essentially the same as Array_intersect (), except that it also considers the key of the array in the comparison. Therefore, only key/value pairs that appear in the first array and in all other input arrays are returned to the result array. The form is as follows:

$arr = Array (' A ' => ' a ', ' B ' => ' B ', ' C ' => ' C ', ' d ' => ' d '); 
$arr 1= Array (' A ' => ' a ', ' C ' => ' B ', ' E '); 
$arr 2= Array (' A ' => ' a ', ' B ' => ' F ', ' d ' => ' B '); 
$arr 3= Array_intersect_assoc ($arr, $arr 1, $arr 2); 
echo "<pre>";
Print_r ($arr 3); 
Print results display:
Array
(
  [a] => a
)

To find the difference set of an associative array
The function Array_diff_assoc () is essentially the same as the Array_diff (), except that it also takes into account the key of the array when it is compared, so that key/value pairs appearing only in the first array and not in other input arrays are returned to the result array. The form is as follows:

$arr = Array (' A ' => ' a ', ' B ' => ' B ', ' C ' => ' C ', ' d ' => ' d '); 
$arr 1= Array (' A ' => ' a ', ' B ' => ' B ', ' e ' => ' e '); 
$arr 3= Array_diff_assoc ($arr, $arr 1); 
echo "<pre>";
Print_r ($arr 3); 
Print results display:
Array
(
  [c] => c
  [d] => D
)

Other useful array functions
Returns a random set of key Array_rand () functions that return one or more keys in an array. In the form of:

$arr = Array (' A ' => ' a ', ' B ' => ' B ', ' C ' => ' C ', ' d ' => ' d '); 
$arr 1= Array_rand ($arr, 2); 
echo "<pre>";
Print_r ($arr 1);
Print results show:
 Array
(
  [0] => C
  [1] => D
)//Each refresh displays a different result

Sum the values in the array
The Array_sum () function adds all the values in the array together, returning the final and the form as follows:

$arr = Array (' A ', +, ' B '); 
$count = Array_sum ($arr); 
echo "<pre>";
Print_r ($count); 

Print results show:
44

If the array contains other data types, such as strings, the values are ignored.

Dividing arrays
The Array_chunk () function decomposes an array into a multidimensional array that consists of multiple arrays that contain the size element. The form is as follows:

$arr = Array (' A ', ' B ', ' C ', ' D '); 
$arr 1= Array_chunk ($arr, 2); 
echo "<pre>";
Print_r ($arr 1); 

Print results show:

Array
(
  [0] => array
    ([
      0] => A
      [1] => B
    )

  [1] => array
    (
      [0] = > C
      [1] => D
    )

)

When processing arrays, callable functions have

Array_filter (*array*,*callbackfunction*);
ARRAY_INTERSECT_UASSOC (*array1*,*array2*,*array3*...,*myfunction*)
Array_intersect_ukey (*array1*,*array2* , *array3*...,*myfunction*)
array_reduce (*array*,*myfunction*,*initial*)
Array_walk (*array*,* myfunction*,*userdata* ...)
......

The above is the entire content of this article, I hope that you learn PHP programming help.

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.