PHP Functions to work with PHP Arrays

By   | on November 12th, 2012

This posts was written in year 2006 on 20th May. In one of my server crash, I lost my posts, Today I got it recovered from Archive.org, and representing it again.

 

Today, I am going to write about array PHP functions, these functions allow you to interact with and manipulate arrays in various ways.
Arrays are essential for storing, managing, and operating on sets of variables.

What are Arrays?

This is very easy to tell but sometime it is really difficult to understand the dynamics of arrays, after functions, arrays are one of the greatest mechanism of programming to solve the problem of data maintenance and presentation.

Arrays are collections of data in a particular way.

To know more about array in programming visit wikipedia page or you can visit php array page to know specifically about PHP arrays.

PHP has many functions to work with arrays, You can view the list and short explanation of their working at http://www.php.net/arrays

In this article I have listed only some very important functions, but it does not mean that other functions are not important , every function has its importance in the context of that’s functions requirement.

Some Array Functions are :-

array_chunk()
This function split an array into chunks, it is useful to divide a large array into smaller once. You may also have an array with less values at the end. You get the arrays as members of a multidimensional array indexed with numbers starting from zero.

array_combine()
Creates an array by using one array for keys and another for its values
Returns FALSE if the number of elements for each array isn’t equal or if the arrays are empty.

array_diff()
This function returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved.

array_flip()
Exchanges all keys with their associated values in an array This function returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

array_merge()
array_merge() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.

array_pop()
This is one of shortest and easy but yet very useful function, this function return the last element of array,. A simple use is to get the extension of any file while using it with explode function.

$ext=array_pop(explode(“.”,”this.is.file.ext”));

array_rand()
Pick one or more random entries out of an array
If you are using random function of php then calling the index as per that value, you can use this function to get random results. One of neat function of PHP.

array_reverse()
Just reverse the over of array elements.

array_search()
Searches the array for a given value and returns the corresponding key if successful

array_slice()
Extract a slice of the array

array_unique()
Removes duplicate values from an array

array_walk()
Apply a user function to every member of an array

extract
I have used this function almost in my every application, using this function you can even simulate the register_globals=On effect in the PHP version where they are Off (They should be off in any ideal PHP installation.)

in_array()
Checks if a value exists in an array

natsort()
Sort an array using a “natural order” algorithm

shuffle()
Shuffle an array

Some very useful functions are listed at http://www.zend.com/codex.php?CID=281. These functions are not in built in the PHP , but they really save the time in need.

  1. No comments yet.
  1. No trackbacks yet.