Follow us on Facebook
Faucibus toroot menuts
PHP Array functions
PHP provides several array functions which is helpful in manipulating array. There are large list of array functions, but below I am providing most used functions list.
Useful Array Function
● array_push()
The array_push() function inserts one or more elements to the end of an array.
Example:
Result:
● array_chunk
array_chunk — Split an array into chunks
Example:
Result:
● array_flip
array_flip — Exchanges all keys with their associated values in an array. In another word, key becomes value and value becomes key.
Example:
Result:
● array_pop
array_pop — Pop the element off the end of array
Example:
Result:
● asort
asort — Sort an array and maintain index association. In another word, the array is arranged in ascending order as per its value and not key.
Example:
Result:
● current
current — Return the current element in an array
Example:
● end
end — Set the internal pointer of an array to its last element
Example:
● sizeof
sizeof — Alias of count()
● sort
sort — Sort an array. Sorts an array as per its key and not value.
Example:
Result :
● Shuffle
shuffle — Shuffle an array
Example:
Result :
It’s like generating random numbers
● ksort
ksort — Sort an array by key
● in_array
in_array — Checks if a value exists in given array or not. It returns Boolean value.
Example:
Result :
● array_diff
Finds difference between two arrays and shows which matches in both the arrays.
Example:
Result:
● array_intersect
This array functions shows intersection of two arrays.
Example:
Result:
● array_unique
This array function is like MySQL unique keyword. Using this function, you can eliminate duplicate array value.
Example:
Result: