Pointer operation of PHP array

Source: Internet
Author: User
Tags array arrays compact numeric range sort

Array ("Key" => "value");

Display array

Print_r ($array);

Use the compact () function to create a new array and make the argument a cell of the new array;

$newArray = Compact ("Red", "green", "yellow", "Blue", "array");

Use the Extract () function to convert a cell in an array to a variable

Extract ($exArray);

echo "$key 1 $key 2 $key 3 $key 4 $key 5";

※ Check value, key

Array_key_exists ($key, $array);//check array key

In_array ($value, $array);//check value to array

※ Get the value

Use Array_values () to get the value of an array

$carValues = Array_values ($car);

Remove the key name of the array

$twoKeys = Array_keys ($two);

Key ($array);//output keys for current cell

After the array is defined, use current () to get the value of the active cell

$red = current ($array);

List ($red, $green) = $array;//Assign the value in the array to the variable, $array = Array ("Red", "green");

each ($two);//returns the key and value of the current cell in the array

※ Traverse Array

foreach ($two as $subArray);//traversal array

while (the list ($key, $value) = each ($array)) {

echo "$key => $value,";//use each to traverse the array

}

※ Filled Array

Populate the array to the left and right

Array_pad ($array, +3, "Shuzhi");//2 parameter is padding from left to right and the value is larger than the number of cells

$array 1 = Array_fill (5,5, "test");//Use Array_fill () to populate the value of this array, the value is test, start with unit 5th, fill 5 cells

Fill Array Key Name

$keys = Array (' String ', 5, ' str ');

$array 3 = Array_fill_keys ($keys, "array value");

Exchanging key names and values using the ARRAY_FILP () function

$speed = Array_flip ($speed);

Use the Array_splice () function to replace the 6th cell with a value of 7

$output = Array_splice ($input, 6,0,7);

Delete an array cell using the Array_splice () function, leaving only the first 5 cells

$output = Array_splice ($input, 5);

$array 1 = range (10,100,10); Set the step value between cells using the third argument of the range () function

※ Sort

Shuffle ($array);//to disrupt array order

To sort three arrays using Array_multisort ()

Array_multisort ($sort 1, $sort 2, $sort 3);

Sort the array and keep the index relationship

Asort ($array);

Reverse-sort the test array and keep the index relationship

Arsort ($array);

Use Ksort () to sort the key names in the array

Ksort ($array);

Reverse sort using the Krsort () function key Name

Krsort ($array);

Sort the test array by using sort () [Arrange by Key name]

Sort ($array);

Use Natsort () sort [natural sort, in numerical order] for cell value case sensitivity

Natsort ($array);

Use the Natcasesort () function to sort [natural sort] but ignore numeric capitalization

Natcasesort ($array);

Use the Array_reverse () function to sort the array cells in the opposite order

$newArray = Array_reverse ($array, TRUE);//true preserve the original key name when set

※ Intersection, Difference set

Use Array_diff () to compute the difference set of three arrays [array numeric comparison]

$result = Array_diff ($dog 1, $dog 2, $dog 3);

Use ARRAY_DIFF_ASSOC () to compute the difference set for a three array [compare values and key names]

$result = Array_diff_assoc ($dog 1, $dog 2, $dog 3);

Use Array_diff_key () to compute the difference set of three arrays [compare key names]

$result = Array_diff_key ($dog 1, $dog 2, $dog 3);

Use Array_intersect () to compute the intersection of three arrays [array numeric comparisons]

$result = Array_intersect ($dog 1, $dog 2, $dog 3);

Use ARRAY_INTERSECT_ASSOC () to compute the intersection of three arrays [compare values and key names]

$result = Array_intersect_assoc ($dog 1, $dog 2, $dog 3);

Use Array_intersect_key () to compute the intersection of three arrays [compare key names]

$result = Array_intersect_key ($dog 1, $dog 2, $dog 3);

※ Merging arrays

Merging arrays using the Array_merge () function

$result = Array_merge ($array 1, $array 2, $array 3, $array 4, $array 5);

Array_rand ($input, 10);//randomly remove 10 units

Count ($array, count_recursive);//show number of array cells, 2 parameters can only be 1 or count_recursive, sometimes traversing multidimensional arrays

※ Access Stack

Array out of Stack, LIFO, the last cell of the array pops up

Array_pop ($array);

Array into the stack, adding 7, 82 values to the tail of the array

Array_push ($array, 7,8);

Move array start cell to group

Array_shift ($array);

Add 7, 8 to the beginning of the array

Array_unshift ($array, 7,8);




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.