After reading these, my mother no longer worried that my face would be green when asked by the array in the PHP interview

1. Basic functions of array operations

Array keys and values

  • array_values($arr); get the value of the array

  • array_keys($arr); Get the keys of the array

  • array_flip($arr); The value in the array is interchanged with the key name (if there is a repeat of the previous one will be overwritten by the latter)

  • in_array("apple",$arr); retrieve apple in the array

  • array_search("apple",$arr); Retrieve apple in the array and return the key name if it exists

  • array_key_exists("apple",$arr); Retrieve whether the given key name exists in the array

  • isset($arr[apple]): Retrieve whether the given key name exists in the array

Internal pointer of array

  • current($arr); returns the current unit in the array

  • pos($arr); returns the current unit in the array

  • key($arr); returns the key name of the current cell in the array

  • prev($arr); Rewind the internal pointer in the array one bit

  • next($arr); Move the internal pointer in the array forward one bit

  • end($arr); Point the internal pointer in the array to the last unit

  • reset($arr; Point the internal pointer in the array to the first unit

  • each($arr); will return a key/value constructed array of the current element of the array, and move the array pointer forward one bit

  • list( k e y , key, k e y , value)=each($arr); Get the key name and value of the current element of the array

Conversion between arrays and variables

  • extract($arr); is used to convert the elements in the array into variables and import them into the current file, with the key name as the variable name and the value as the variable value

Note: (The second parameter is very important and can be used in the manual) How to use echo $a;

  • compact(var1,var2,var3); Create an array with the given variable name

Second, the segmentation and filling of the array

Segmentation of the array

  • array_slice($arr,0,3); You can take out a section of the array, this function ignores the key name

  • array_splice($arr,0,3,array("black","maroon")); You can take out a section of the array. The difference from the previous function is that the returned sequence is deleted from the original array

Split multiple arrays

  • array_chunk($arr,3,TRUE); You can split an array into multiple, TRUE is to keep the key of the original array

Filling of the array

  • array_pad($arr,5,'x'); fill an array to the specified length

Three, array and stack

  • array_push($arr,"apple","pear"); Push one or more elements into the end of the array stack (into the stack), and return the number of elements pushed into the stack

  • array_pop($arr); Pop the last element of the array stack (pop the stack)

Four, arrays and queues

  • array_shift($arr); The first element in the array is moved out and returned as the result (the length of the array is reduced by 1, the other elements are moved forward by one, the numeric key name is changed from zero technology, and the text key name remains unchanged)

  • array_unshift($arr,"a",array(1,2)); insert one or more elements at the beginning of the array

Five, callback function

  • array_walk($arr,'function','words'); Use the user function to process each member in the array (the third parameter is passed to the callback function function)

  • array_mpa(“function”, a r r 1 , arr1, a r r 1 , arr2); can handle multiple arrays (when using two or more arrays, their length should be the same)

  • array_filter($arr,"function"); Use the callback function to filter each element in the array. If the callback function is TRUE, the current element of the array will be included in the returned result array, and the key name of the array remains unchanged

  • array_reduce($arr,"function","*"); Converted to a single value function (* is the first value of the array)

Six, the sorting of the array

Sort array by element value

  • sort($arr); Sort from small to large (the second parameter is how to sort) array sort ignoring the key name

  • rsort($arr); Sort from largest to smallest (the second parameter is how to sort) Array sort ignoring the key name

  • usort($arr,"function"); Use a user-defined comparison function to sort the values ​​in the array (there are two parameters in the function, 0 means equal, a positive number means the first is greater than the second, and a negative number means the first One is smaller than the second) Array sort ignoring the key name

  • asort($arr); Sort from smallest to largest (the second parameter is how to sort) the array sort that retains the key name

  • arsort($arr); Sort from largest to smallest (the second parameter is how to sort) the array sort that retains the key name

  • uasort($arr,"function"); Use a user-defined comparison function to sort the values ​​in the array (there are two parameters in the function, 0 means equal, a positive number means the first is greater than the second, and a negative number means the first One is smaller than the second) to preserve the key name of the array sort

Sort array by key

  • ksort($arr); Sort in positive order by key name

  • krsort($arr); Sort in reverse order by key name

  • uksort($arr,"function"); Use a user-defined comparison function to sort the keys in the array (there are two parameters in the function, 0 means equal, a positive number means the first is greater than the second, and a negative number means The first is smaller than the second)

Natural ordering

  • natsort($arr); Natural sorting (ignoring key names)

  • natcasesort($arr); Natural sort (ignore case, ignore key names)

Seven, the calculation of the array

Sum of array elements

  • array_sum($arr); Sum all the elements inside the array

Combination of arrays

  • array_merge($arr1,$arr2); Merge two or more arrays (the same string key name, the latter overwrite the previous one, the same numeric key name, the latter will not be overwritten, but appended to the back)

  • "+"$ arr1+$arr2; For the same key name, only the last one is reserved

  • array_merge_recursive($arr1,$arr2); Recursive merge operation, if the array has the same string key name, these values ​​will be merged into an array. If a value itself is an array, it will be merged into another array according to the corresponding key name. When the array has the same array key name, the latter value will not overwrite the original value, but will be appended to the back

Difference of array

  • array_diff( a r r 1 , arr1, a r r 1 , arr2); returns the difference result array

  • array_diff_assoc( a r r 1 , arr1, a r r 1 , arr2,$arr3); returns the difference result array, and the keys are also compared

Intersection of arrays

  • array_intersect( a r r 1 , arr1, a r r 1 , arr2); returns an array of intersection results

  • array_intersect_assoc( a r r 1 , arr1, a r r 1 , arr2); returns an array of intersection results, and the keys are also compared

Eight, other array functions

  • range(0,12); Create an array containing the specified range of cells

  • array_unique($arr); Remove duplicate values ​​in the array, the new array will retain the original key name

  • array_reverse($arr,TRUE); Returns an array with elements in the reverse order of the original array, if the second parameter is TRUE, the original key name is retained

  • //srand((float)microtime()*10000000); random seed trigger

  • array_rand($arr,2); randomly remove one or more elements from the array

  • shuffle($arr); Shuffle the order of the array

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108856111
Recommended