PHP Array Common functions

Source: Internet
Author: User

Functions commonly used in PHP arrays:

1.//array_values-returns all values in the array 

$array = Array ("name" = "Zhangsan", "age" = "ten", "sex" = "man");  $tem = Array_values ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";
The output results are as follows:

  

2. returns some or all of the key names in the array  
$array = Array ("name" = "Zhangsan", "age" = "ten", "sex" = "man");  $tem = Array_keys ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";

The output results are as follows:

  


3. Check if a value exists in the array, returns the value of a Boolean type (BOOL)  
$array = Array ("name" = "Zhangsan", "age" = "ten", "sex" = "man");  $tem = In_array ("Ten", $array, false);  echo "<pre>";  Print_r ($tem); echo "</pre>";
Where the third parameter defaults to FALSE and returns 1 if it exists, no return value is present. If set to true, checks whether the type is the same, which is equivalent to the "= = =" effect.

4. exchanging keys and values in an array
$array = Array ("name" = "Zhangsan", "age" = "ten", "sex" = "man");  $tem = Array_flip ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";
The output results are as follows:
  

String If the value is of the wrong type, a warning will be issued and the problematic key/value pair will not be flipped.

If the same value occurs more than once, the last key will be its value, and all the others are lost.

5.array_reverse-Returns an array of cells in reverse order, for example :
$array = Array ("Zhangsan", 5, "Ten", "sex" = "man");   $tem = Array_reverse ($array);   echo "<pre>";   Print_r ($tem); echo "</pre>";
  
Note that if the second parameter here is set to true, the key of the number is preserved. Non-numeric keys are not affected by this setting and are always retained. Cases:
$array = Array ("Zhangsan", 5, "Ten", "sex" = "man");  $tem = Array_reverse ($array, true);  echo "<pre>";  Print_r ($tem); echo "</pre>";
Here's the second parameter, set to True, as shown in:

  

6.count- calculates the number of cells in an array or the number of attributes in an object, for example:

$array = Array ("Zhangsan", 5, "Ten", "sex" = "man");  $tem = count ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";
Here the $tem is the number of arrays, the number is 4;

7. Count the number of occurrences of all values in the array, for example:
$array = Array ("Zhangsan", 5, "Ten", "sex" = "man", 5);  $tem = count ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";
As shown in the following:
  


8. array as input and returns a new array with no duplicate values. For example:
$array = Array ("Zhangsan", 5, "Ten", "sex" = "man", 5);  $tem =array_unique ($array);  echo "<pre>";  Print_r ($tem); echo "</pre>";
As shown in the following:

  

9.array_filter- uses a callback function to filter the cells in the array, for example: Filter the array below 3 in the following array.

$array = Array (1,2,3,4,5);  The callback functions are as follows function filter ($val) {    if ($val >3) {    return $val;    }}//returns the filtered array.  $tem = Array_filter ($array, "filter");  echo "  <pre>";  Print_r ($tem); echo "</pre>";
The results are as follows:

  

array_walk- uses a user-defined function to do callback processing for each element in the array. Returns TRUE when successful, or on Failure FALSE .

$array = Array (1,2,3,4,5);
The callback functions are as follows function Walk ($val) {    $val = $val * $VAL;
No return value}//use custom function, let each element do callback processing $tem = Array_walk ($array, "Walk"); echo "<pre> ";p rint_r ($tem); echo" </ Pre >";

array_map-Apply a callback function to each element of an array

$array = Array (1,2,3,4,5);
The //callback functions are as follows function map ($val) {    $val = $val * $val;    return $val;//return value}//use a custom function, let each element do callback processing and form a new array $tem = Array_map ("map", $array); echo "<Pre  >";p rint_r ($tem); echo"</pre>";

The original array also exists, returning a new array, returning the result as shown:

Here the difference between Array_map and Array_walk, Array_walk is to make each element in the array to do callback processing, do not return a value, Array_map is to make each element in the array callback processing, and return a new array.

sort-. The array is sorted in ascending order. Sort () example

$array = Array (10,2,13,4,50);
sorting an array in ascending order
$tem = sort ($array);
echo "<pre>";
Print_r ($tem);//function return value successfully
echo "<br>";
Iterate through the sorted array
foreach ($array as $key = = $val) {
echo "Array [". $key. "] = " . $val. "\ n";
}

echo "</pre>";
Returns  TRUE when successful, or on Failure  FALSE . As shown in the following:

rsort-Array reverse order (Descending) Rsort () example
  $array = Array (10,2,13,4,50);//descending sort of array $tem = Rsort ($array); echo " <  pre  >   ";p Rint_r ($tem);//function return value echo"  <  br  >   array  [". $key. "] = " . $val. "\ n"; 
}
Echo " </ pre > ";
returns TRUE when successful, or  FALSE on Failure  . As shown in the following :


Other sorting methods are as follows:


array_slice- .Remove a paragraph from the array. Example:
$array = Array (10,2,13,4,50,15);
Intercept index 1 starts with an array element of length 5 $tem = Array_slice ($array, 1,5); Echo<pre> ";p Rint_r ($tem); echo"</pre>";

As shown in the following:

array_splice-. Remove part of the array and replace it with other values. Array_splice Example

$array = Array (1,2,3,4,5);
The Intercept index starts with 2 minus the 1-length array element and replaces it with the element 6来 3, noting that Array_splice cannot have more than four parameters, such as adding more elements, creating a new array, putting the elements that need to be added into the new array,
Then put the new array into the Array_splice fourth parameter
Array_splice ($array, 2,1,6);
echo "<pre>";
Print_r ($array);
echo "</pre>";

The results are as follows:

array_combine-(merging two arrays) creates an array with the value of one array as its key name and the value of the other array as its value. Example:

Array 1
$array = Array (1,2,3,4);
Array 2$array2 = Array ("Zhangsan", "Lisi", "Wangwu", "Zhaoli");
Merge Arrays $tem =array_combine ($array, $array 2); echo "<pre>";p rint_r ($tem); echo "</pre>";

Results such as:

array_merge- Merging one or more arrays

//Array 1$array = Array (1,2,3,4),//Array 2$array2 = Array ("Zhangsan", "Lisi", "Wangwu", "Zhaoli");//merge array $tem =array_merge ( $array, $array 2), echo "<pre>";p rint_r ($tem); echo "</ Pre >";

Results such as:

array_intersect- computes the intersection of an array. Example:

//Array 1$array = Array (1,2,3,4,5);//Array 2$array2 = Array (3,4,5,6,7);//Intersection $tem =array_intersect ($array, $array 2); echo "  <pre>";p rint_r ($tem); echo"</pre> ";

Results such as:

array_diff- computes the difference set of an array.

Note that the value returned from the first array is different from the second array, and the values in the second array are not returned.

Example:

//Array 1$array = Array (1,2,3,4,5);//Array 2$array2 = Array (3,4,5,6,7);//Intersection $tem =array_diff ($array, $array 2); echo " < Pre > ";p rint_r ($tem); echo" </ Pre >";

20.array_pop-pops the last element of the array (out of the stack). Return the last value, note:

Returns array the last value. If array it is empty (if it is not an array), NULL it will be returned.

$array = Array (1,2,3,4,5); $tem =array_pop ($array); echo " < Pre > ";p rint_r ($tem);//Returns the last value is 5echo" </ Pre >";

array_push-pushes one or more cells into the end of the array (into the stack). Returns the number of elements of the array after processing.

$array = Array (1,2,3,4,5); $tem =array_push ($array, 6); echo " < Pre > ";p rint_r ($tem);//Returns the number of elements of the array after processing. echo "</pre>";

array_shift-. Move the cells at the beginning of the array to a group. Note that returns the value that is moved out if array it NULL is empty or not an array.  

$array = Array (1,2,3,4,5); $tem =array_shift ($array); echo " < Pre > ";p rint_r ($tem); Returns the value of the moved out to 1

echo " </ Pre >";

array_unshift- Inserts one or more cells at the beginning of the array

$array = Array (1,2,3,4,5); $tem =array_unshift ($array); echo " < Pre > ";p rint_r ($tem); Returns the number of elements in an array

echo " </ Pre >";

24.list ();

is deconstructed in the order of index corners, but the corner mark must start at 0 and be continuous

Valid only for indexed arrays, which are skipped directly when an associative array is encountered

$array 5 = Array ("name" = "Zhangsan", 0 = "Lisi", 1 = "Wangwu"), List ($a, $b) = $array 5;echo $a. " ". $b;  The result is Lisi,wangwu

25.each ();

Returns an array of the first key-value pairs (the mixed form of the index and the associative array),

When the second call is, the cursor is automatically to the next, that is, the second key value of the array;

$array 6 = Array ("name" = "Fangming", 0 = "+", "sex" = "man"), $iem 1 = each ($array 6);p Rint_r ($iem 1); echo "
    <br>"; $iem 2 = each ($array 6);p Rint_r ($iem 2); 

26.list,each combined use

list ($a, $b) = each ($array 6), echo $a. ":". $b. " < BR >  "; 

27. Traversing with a while loop combined with List,each

$array 7 = Array ("name" = "fangming", "age" = "+", "sex" = "man"), and while (list ($a, $b) = each ($array 7)) {    echo $a. ":". $b. " < BR > ";};

28.reset, resets the array so that the cursor returns to the first bit

Reset ($array 7);
























PHP Array Common functions

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.