PHP Arrays

Posts about arrays in PHP

PHP Logo

PHP Array Of UK Counties

4th March 2009 - 1 minute read time

Use the following code to create an array of counties in the UK.

PHP Logo

Serialize And Unserialize With PHP

30th June 2008 - 2 minutes read time

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.

PHP Logo

The PHP array_flip() Function And Detecting Functions

26th June 2008 - 2 minutes read time

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.

PHP Logo

Sorting A Multidimensional Array With PHP

9th June 2008 - 4 minutes read time

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.

PHP Logo

Remove Blank Entries From An Array With PHP

2nd June 2008 - 2 minutes read time

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.

PHP Logo

Split An Array Into Smaller Parts In PHP

17th May 2008 - 2 minutes read time

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.

PHP Logo

HTML Checkbox To PHP Array

3rd April 2008 - 2 minutes read time

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.

PHP Logo

Array Sorting Algorithms In PHP

1st April 2008 - 7 minutes read time

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.

PHP Logo

PHP array_merge() Function Improvement

31st March 2008 - 2 minutes read time

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.