PHP Array Cheatsheet


Cheatsheet for PHP Arrays

The following cheatsheet can be used to remind you of many common array functions that are used with PHP.

   
array_​change_​key_​case
    -changes the array key to upper or lower case
   
array_​chunk
    -makes smaller arrays based on how many items you specify to chunk off. For example, if you have an array of 5 items, it would be two arrays with two items and one array with 1 item.
   
array_​column
    -Can specify a key and it returns an array of only those keys and matching values. The outcome is a single indexed array.
   
array_​combine
    -combines 2 arrays for which one will become keys and the other will become values. The first array will be the keys and the second array will be the values.
   
array_​count_​values
    -counts the number of values in an array. If there are two identical items, that value would be the key and the value would be 2.
   
array_​diff_​assoc
    -returns the different key => value pairs.
   
array_​diff_​key
    -returns ann array of the keys that exist in array 1 that are not present in array 2.
   
array_​diff_​uassoc
    -compares two arrays and returns the differences. Can check keys and values.
   
array_​diff_​ukey
    -compares 2 arrays can return different keys in array 1 that do not exist in array 2.
   
array_​diff
    -compares values of 2 arrays. Keys are not important.
   
array_​fill_​keys
    - turns an array into keys and create a value that you specify.
   
array_​fill
    - create an indexed array with specific keys and create a value for those keys.
   
array_​filter
    - can filter an array to get specific values; like all even or add numbers. By default, it will remove false, NULL and empty values.
   
array_​flip
    -converts the key to a value and vice versa.
   
array_​intersect_​assoc
    -returns the matches for keys and value pairs in both arrays.
   
array_​intersect_​key
    -returns the matching keys of both arrays.
   
array_​intersect_​uassoc
    -returns matching values of two arrays.
   
array_​intersect_​ukey
    -returns matching keys
   
array_​intersect
    -returns values that match in both arrays.
   
array_​key_​exists
    -checks is key exists in an array.
   
array_​keys
    -returns the array keys of the desired array.
   
array_​map
    -Can apply a callback function to perform a function on all items in array. Can use with 2 arrays; like passing 2 values into a custom function.
   
array_​merge_​recursive
    -merges 2 arrays. If the first array and second array have matching keys, the items in the second array will add on to the key from the first array. Then, the other items will be added in sequence.
   
array_​merge
    -Merges arrays. But, if there are matching keys the last one will override. 
   
array_​multisort
    -sort multiple and multidimensional arrays in a desired sequence.
   
array_​pad
    -Adds items before or after the current items. Negative numbers are added before, positive size is added after the current list.
   
array_​pop
    -pops the last item off the array so that you end up with a new array with the popped off item.   
   
array_​product
    -Takes the numbers in array and multiplies them; like removing the comma with a multipication sign.
   
array_​push
    -add items to the end of an array.
   
array_​rand
    -get a random item from an array.
   
array_​reduce
    -reduce an array to a sibgle item using a callback function that can do things like add or multiply all items in an array.
   
array_​replace_​recursive
    -the second array will replace items from the first array. keys from second array will have value override that value from teh first array. Any new keys will be added. 
   
array_​replace
    -Add or replace items from first array.
   
array_​reverse
    -reverses order of array. But, if thhere is an array within the array to be reversed, its values stay as they are.
   
array_​search
    -search for a value and returns the key of that value.
   
array_​shift
    -like array_pop, except the first item is removed from the array.
   
array_​slice
    -remove specified items from array.
   
array_​splice
    -select specified items from an array.
   
array_​sum
    -add all number values in array.
   
array_​udiff_​assoc
    -compare difference in 2 arrays with an index check and by using a callback function.
   
array_​udiff_​uassoc
    -compare difference in 2 arrays with an index check. The data and indexs are compared with a callback function.
   
array_​udiff
    -compare difference in 2 arrays using a callback function.
   
array_​uintersect_​assoc
    -compare the union in 2 arrays with an index check and by using a callback function.
   
array_​uintersect_​uassoc
    -compare union in 2 arrays with an index check. The data and indexs are compared with a callback function.
   
array_​uintersect
    -compare union in 2 arrays using a callback function.
   
array_​unique
    -returns an array without duplicated values. The first value will hold the position in the new array.
   
array_​unshift
    -add items to the beginning of an array.
   
array_​values
    -returns an indexed array with only the values. The orignal keys are lost.
   
array_​walk_​recursive
    -you can apply a function to each key and value in an array. Also works with arrays within arrays.
   
array_​walk
    -you can apply a function to each key and value in an array.
   
array
    -used to lists items of indexed, associative and multidemensional arrays.
  
compact
    -Create an array from variables where the variable name becomes the key and the string value becomes the value.
   
count
    -counts items within an array
   
current
next
prev
end
    -The 4 above functions can be used to grab a specific value from an array. If one is called after another it performs the function based on the last item position.
pos
    -alias of current. Grabs the current value from an array. If this is the first command after an array is set, the current item is the first one.
  
  each 
    -often used with while (list($key, $val) = each($fruit)) { }. This gives key and value of each item in an array.  
    extract
    -turns an array into individual strings where the key becomes the variable name and the value is the same as that from the array.
   
in_​array
    -checks if a string exists in a given array.
   
key_​exists
    -checks if an array key exists.
   
key   
    -gets the key name after checking the value in order to make a match.
   
list
    -makes variables based on values in an array. The names can be anything while the value is the same as the one from the array. If an array had 3 values, you would likely list 3 variables.
   
natcasesort
    -sorts an array based on a 'natural order' algorithm. Large case will order ahead of small case.
   
range
    -cerate a new array based on the desired range. For example, you may want items 2-8 in an array with 20 items.
   
reset
    -often used with functions like current, next, prev and end. it repositions to the first item in the array just as though you are dealing with a fresh array that has had no functions applied to it.
   
shuffle
    -shuffles the array and makes it random.
   
sizeof
    -same as count. Returns the number of items in an array. Works recursivley. So, if you have an array with 2 arrays, it returns a count for the two arrays and counts for each sub item.
   
    Various Soring Functions Below
    natsort   
    arsort
    asort
    krsort
    ksort
    rsort   
    sort
    uasort
    uksort
    usort