PHP array pointer operation code

  • 2020-03-31 21:30:10
  • OfStack


Mixed prev (array & $arr);
< ? PHP
$transport = array (' foot ', 'bike', 'car', 'plane');
$mode = current ($transport); / / $mode = 'foot';
$mode = next ($transport); / / $mode = 'bike';
$mode = next ($transport); / / $mode = 'car';
$mode = prev ($transport); / / $mode = 'bike';
$mode = end ($transport); / / $mode = 'plane';
$mode = reset ($transport); / / $mode = 'foot';
? >

Array (" key "= >" Value "); Build array
// displays an array
Print_r ($array);
// use the compact() function to create a new array and use the parameters as elements 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";


does check the value and key
Array_key_exists ($key, $array); // check the array keys
The in_array ($value, $array); // check the value in the array

Does get the value
// use array_values() to get the values of the array
$carValues = array_values ($car);
// fetch the key name of the array
$twoKeys = array_keys ($two);
The key ($array); // outputs the key name of the current cell
// after the array is defined, use current() to get the value of the current cell
$red = current ($array);
The list ($red, $green) = $array; $array = array(" red "," green ");
Each ($two); // returns the key and value of the current cell in the array

does iterate over groups
The foreach ($two as $subArray); // go through the groups
While (the list ($key, $value) = each ($array)) {
Echo "$key = > $value "; // use each to iterate through the groups
}

does fill the array
// fill the array left and right
Array_pad ($array, + 3, "shuzhi"); //2 parameter is filling from left to right, and the value is greater than the number of cells
$array1 = array_fill (5, 5, "test"); // use array_fill() to fill the value of this array, which is test, starting from the 5th cell, and filling a total of 5 cells
// fill the array key name
$keys = array('string', 5, 10, 'STR ');
$array3 = array_fill_keys($keys," array value ");
// use the array_filp() function to swap the key name and value
$speed = array_flip ($speed);
// replace the value of cell 6 with 7 using the array_splice() function
$output = array_splice ($input, 6,0,7);
// use the array_splice() function to delete the array cells, leaving only the first five
$output = array_splice ($input, 5);
$array1 = range (10100, 10); // use the third parameter of the range() function to set the step value between cells


Does the sorting
The shuffle ($array); // shuffles the array
// sort the three arrays using array_multisort()
Array_multisort ($sort1, $sort2, $sort3);
// sort the array and keep the index
Asort ($array);
// sort the test array in reverse and keep the index relationship
The arsort ($array);
// sort array key names using ksort()
The ksort ($array);
// reverse sort using the krsort() function
The krsort ($array);
// sort the test array using sort()
Sort ($array);
// use natsort() sort to be case-sensitive to cell values
The natsort ($array);
// sort with the natcasesort() function but ignore numeric case
Natcasesort ($array);
// USES the array_reverse() function to sort, and the array elements are arranged in reverse order
$newArray = array_reverse ($array, TRUE); //TRUE is set to keep the original key name

does intersection, difference set
// calculate the difference set of three arrays using array_diff()
$result = the array_diff ($dog1, $dog2, $dog3);
// calculate the difference set of three arrays using array_diff_assoc()
$result = array_diff_assoc ($dog1, $dog2, $dog3);
// calculate the difference set of three arrays using array_diff_key()
$result = array_diff_key ($dog1, $dog2, $dog3);
// compute the intersection of three arrays using array_intersect()
$result = array_intersect ($dog1, $dog2, $dog3);
// calculate the intersection of three arrays using array_intersect_assoc() [compare values and key names]
$result = array_intersect_assoc ($dog1, $dog2, $dog3);
// calculate the intersection of three arrays using array_intersect_key()
$result = array_intersect_key ($dog1, $dog2, $dog3);

does merge array
// merges the array using the array_merge() function
$result = array_merge ($array1, $array2, $array3, $array4, $array5);
The array_rand ($input, 10); // pick 10 cells at random
The count ($array, COUNT_RECURSIVE); // displays the number of array cells, 2 arguments can only be 1, or COUNT_RECURSIVE, and sometimes multidimensional arrays can be traversed

Does in the stack
// array out, last in, first out, the last cell of the array pops up
The array_pop ($array);
// array push to add 7,8 values to the end of the array
Array_push ($array, 7,8);
// moves the array header out of the array
The array_shift ($array);
// adds 7,8 to the beginning of the array
The array_unshift ($array, 7, 8);

Related articles: