Php array pointer operations

Source: Internet
Author: User
Array (key = value); create an array // display the array print_r ($ array); // use the compact () function to create an array and use the parameter as the unit of the new array; $ newArray = compact (red, green, yellow, blue, array); // use the extract () function to convert the elements in the array to the variable ex

Array ("key" => "value"); creates an array

// Display an array

Print_r ($ array );

// Use the compact () function to create an array and use parameters as the unit of the new array;

$ NewArray = compact ("red", "green", "yellow", "blue", "array ");

// Use the extract () function to convert the elements in the array into variables.

Extract ($ exArray );

Echo "$ key1 $ key2 $ key3 $ key4 $ key5 ";

※Check value and key

Array_key_exists ($ key, $ array); // Check the array key

In_array ($ value, $ array); // check the value in the array

※Get value

// Use array_values () to obtain the array value

$ CarValues = array_values ($ car );

// Retrieve the key name of the array

$ TwoKeys = array_keys ($ two );

Key ($ array); // key name of the current output unit

// After the array is defined, use current () to obtain the value of the current unit.

$ Red = current ($ array );

List ($ red, $ green) = $ array; // assign values in the array to variables, $ array = array ("red", "green ");

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

※Traversing an array

Foreach ($ two as $ subArray); // traverses the array

While (list ($ key, $ value) = each ($ array )){

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

}

※Fill an array

// Fill in the array to the left and right

Array_pad ($ array, + 3, "shuzhi"); // the value 2 is filled from left to right. it is filled when the value is greater than the number of units.

$ Array1 = array_fill (5, 5, "test"); // use array_fill () to fill in the value of this array. The value is test, which is filled from 5th units. a total of five units are filled.

// Fill in the array key name

$ Keys = array ('string', 5, 10, 'str ');

$ Array3 = array_fill_keys ($ keys, "Array Value ");

// Use the array_filp () function to exchange key names and values

$ Speed = array_flip ($ speed );

// Use the array_splice () function to replace the value of the first unit with 7

$ Output = array_splice ($ input, 6, 0, 7 );

// Use the array_splice () function to delete the array unit. only the first five units are retained.

$ Output = array_splice ($ input, 5 );

$ Array1 = range (10,100, 10); // use the third parameter of the range () function to set the step value between units.

※Sort

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

// Use array_multisort () to sort the three arrays

Array_multisort ($ sort1, $ sort2, $ sort3 );

// Sort the array and maintain the index relationship.

Asort ($ array );

// Sort the test array in reverse order and maintain the index relationship

Arsort ($ array );

// Sort the array key names using ksort ()

Ksort ($ array );

// Use the krsort () function to sort the keys in reverse order.

Krsort ($ array );

// Sort the test array using sort () [sort by key name]

Sort ($ array );

// Use natsort () to sort [natural sorting, in numerical order] case sensitive to unit values

Natsort ($ array );

// Use the natcasesort () function to sort [natural sorting] but ignore the case sensitivity of the value

Natcasesort ($ array );

// Use the array_reverse () function to sort the array units in reverse order.

$ NewArray = array_reverse ($ array, TRUE); // retain the original key name when set to TRUE

※Intersection and difference set

// Use array_diff () to calculate the difference set of the three arrays [comparison of array values]

$ Result = array_diff ($ dog1, $ dog2, $ dog3 );

// Use array_diff_assoc () to calculate the difference set of the three arrays [comparison of values and key names]

$ Result = array_diff_assoc ($ dog1, $ dog2, $ dog3 );

// Use array_diff_key () to calculate the difference set of the three arrays [comparison key name]

$ Result = array_diff_key ($ dog1, $ dog2, $ dog3 );

// Use array_intersect () to calculate the intersection of the three arrays [comparison of array values]

$ Result = array_intersect ($ dog1, $ dog2, $ dog3 );

// Use array_intersect_assoc () to calculate the intersection of the three arrays [comparison of values and key names]

$ Result = array_intersect_assoc ($ dog1, $ dog2, $ dog3 );

// Use array_intersect_key () to calculate the intersection of the three arrays [comparison key name]

$ Result = array_intersect_key ($ dog1, $ dog2, $ dog3 );

※Merge arrays

// Use the array_merge () function to merge arrays

$ Result = array_merge ($ array1, $ array2, $ array3, $ array4, $ array5 );

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

Count ($ array, COUNT_RECURSIVE); // display the number of array units. parameter 2 can only be 1 or COUNT_RECURSIVE, and sometimes can traverse multi-dimensional arrays.

※Warehouse receiving/picking stack

// Array output stack. The last unit of the array is displayed.

Array_pop ($ array );

// Add two values, 7 and 8, to the end of the array

Array_push ($ array, 7,8 );

// Remove the elements starting with the array from the array

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.