How these Php Array Functions will make you better developer?

Sharing is caring!

332 Views -

An array is a data structure that can hold one or ore similar types of data in a single value.

There are three types of an array-
1-Indexed Array
2-Associative Array
3-Multi-dimensional Array

Creates an array

array()
ex.
Syntax for indexed arrays:
array(value1, value2, value3, etc.)

Syntax for associative arrays: 
array(key=>value,key=>value,key=>value,etc.)

Changes all keys in an array to lowercase or uppercase

ex. array_change_key_case(array, case)
CASE_LOWER, CASE_UPPER

array_change_key_case()

array_chunk()

Splits an array into chunks of arrays

ex. array_chunk(array, size, preserve_key)

array_column()

Returns the values from a single column in the input array

ex.array_column(array, column_key, index_key)

array_combine()

Creates an array by using the elements from one “keys” array and one “values” array

ex. array_combine(keys, values)

array_count_values()

Counts all the values of an array

ex. array_count_values(array)

array_diff()

Compare arrays, and returns the differences (compare values only)

ex. array_diff(array1, array2, array3, ...)

array_diff_assoc()

Compare arrays, and returns the differences (compare keys and values)

ex.array_diff_assoc(array1,array2,array3...)

array_diff_key()

Compare arrays, and returns the differences (compare keys only)

ex. array_diff_key(array1, array2, array3, ...)

array_diff_uassoc()

Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)

ex. array_diff_uassoc(array1, array2, array3, ..., myfunction)

array_diff_ukey()

Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)

ex. array_diff_ukey(array1, array2, array3, ..., myfunction)

array_fill()

Fills an array with values

ex. array_fill(index, number, value)

array_fill_keys()

Fills an array with values, specifying keys

ex. array_fill_keys(keys, value)

array_filter()

Filters the values of an array using a callback function

ex.array_filter(array, callbackfunction, flag)

array_flip()

Flips/Exchanges all keys with their associated values in an array

ex. array_flip(array)

array_intersect()

Compare arrays, and returns the matches (compare values only)

ex. array_intersect(array1, array2, array3, ...)

array_intersect_assoc()

Compare arrays and returns the matches (compare keys and values)

ex. array_intersect_assoc(array1,array2,array3, ...)

array_intersect_key()

Compare arrays, and returns the matches (compare keys only)

ex. array_intersect_key(array1, array2, array3, ...)

array_intersect_uassoc()

Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)

ex. array_intersect_uassoc(array1, array2, array3, ..., myfunction)

array_intersect_ukey()

Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function)

ex. array_intersect_ukey(array1, array2, array3, ..., myfunction)

array_key_exists()

Checks if the specified key exists in the array

ex. array_key_exists(key, array)

array_keys()

Returns all the keys of an array

ex. array_keys(array, value, strict)

array_map()

Sends each value of an array to a user-made function, which returns new values

ex. array_map(myfunction, array1, array2, array3, ...)

array_merge()

Merges one or more arrays into one array

ex. array_merge(array1, array2, array3, ...)

array_merge_recursive()

Merges one or more arrays into one array recursively

ex. array_merge_recursive(array1, array2, array3, ...)

array_multisort()

Sorts multiple or multi-dimensional arrays

ex. array_multisort(array1, sortorder, sorttype, array2, array3, ...)

array_pad()

Inserts a specified number of items, with a specified value, to an array

ex. array_pad(array, size, value)

array_pop()

Deletes the last element of an array

ex. array_pop(array)

array_product()

Calculates the product of the values in an array

ex.array_product(array)

array_push()

Inserts one or more elements to the end of an array

ex. array_push(array, value1, value2, ...)

array_rand()

Returns one or more random keys from an array

ex. array_rand(array, number)

array_reduce()

Returns an array as a string, using a user-defined function

ex. array_reduce(array, myfunction, initial)

array_replace()

Replaces the values of the first array with the values from following arrays

ex. array_replace(array1, array2, array3, ...)

array_replace_recursive()

Replaces the values of the first array with the values from following arrays recursively

ex. array_replace_recursive(array1, array2, array3, ...)

array_reverse()

Returns an array in the reverse order

ex. array_reverse(array, preserve)

array_search()

Searches an array for a given value and returns the key

ex. array_search(value, array, strict)

array_shift()

Removes the first element from an array, and returns the value of the removed element

ex. array_shift(array)

array_slice()

Returns selected parts of an array

ex. array_slice(array, start, length, preserve)

array_splice()

Removes and replaces specified elements of an array

ex. array_splice(array, start, length, array)

array_sum()

Returns the sum of the values in an array

ex. array_sum(array)

array_udiff()

Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function)

ex. array_udiff(array1, array2, array3, ..., myfunction)

array_udiff_assoc()

Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)

ex. array_udiff_assoc(array1, array2, array3, ..., myfunction)
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments