Use the following function to remove all duplicate values in an array.
If you have an object or array that you want to save until a later you can use the serialize() and unserialize() functions. The operation of the functions are straightforward. To serialize() an array just pass the serialise function the array like this.
The array_flip() function in PHP is used to swap the values of an array with the keys. Take the following array.
$array = array('key1'=>'value1', 'key2'=>'value2');
To exchange all the values with the keys we pass it through the array_flip() function.
Sorting an array is easy in PHP thanks for the built in sorting functions. However, when it comes to sorting a multidimensional array you need to employ these functions in a certain way, especially if you want to vary the data item you want to sort by.
Take the following defined array, taken from the top news stories in the Science and Nature section of the BBC website.
If you have an array that you want to remove any null data items from then you can use the following function. It will create a new array and only copy across items from the existing array if they contain a value. If the value is an array the function calls itself and makes sure that the returned array contains something before adding it to the new array.
Splitting an array into sections might be useful for setting up a calendar or pagination on a site. Either way there are numerous ways to do this but the following seems to be the quickest and most reliable method.
To create a simple HTML check box use the following bit of code.
<input type="checkbox" name="option2" value="Milk" />
To set the checkbox as filled in include a checked attribute. To make the control XHTML compliant you will need to do the following.
There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations, especially when sorting things like dates as PHP usually guesses which value is higher than the other and can produce odd results. However, there are plenty of sorting algorithms available than can allow you to sort an array in any way you want.
The simplest of these is called the bubble sort. Here is a function that will sort an array of values using the bubble sort algorithm.
The array_merge() function in PHP is a handy way of adding one or more arrays together. Here is an example of how to use it.
Working out the average of a bunch of values is quite a common task, but rather than looping through the array, adding together values as you go and the using the count() function to find out the average at the end.