PHP Arrays

Bogo Sort In PHP

23rd June 2018

I came across this sorting algorithm the other day called 'bogo sort'. This is a sort of joke sorting algorithm that is highly ineffective at actually sorting arrays. The name comes from the word 'bogus'.

Here is how it works.

An Implementation Of Array Binary Search In PHP

19th June 2018

I have been doing some reading and watching lectures of programming theory recently and I was reminded of this algorithm I learned about in university. Binary searching an array is a divide and conquer algorithm that takes an array and searches for a value in that array by splitting the array into halves. The algorithm works like this.

Finding The First And Last Items In An Array In PHP

24th March 2013

Getting the first or last item from an array in PHP is usually quite easy. If you create an array and then add a bunch of values to it then the array index will increment by 1 for every element you add. This means that in order to get the first element from an array you just reference the default starting position (0). To get the last item in the array the index key will be the length of the array, minus 1. Here is some example code showing this.

Using list() With explode() In PHP

8th September 2009

A simple way to convert a string into a set of variables is through the use of the explode() and list() functions. list() is a language construct (not really a function) that will convert an array into a list of variables. For example, to convert a simple array into a set of variables do the following:

Print Array Without Trailing Commas In PHP

I have previously talked about Removing commas from the end of strings, but it is also possible to use the implode() function to do the same sort of thing.

implode() takes two parameters, the separator and the array, and returns a string with each array item separated with the separator. The following example shows how this function works.

philipnorton42 Fri, 04/24/2009 - 09:10
PHP Array Of Canadian States

Use the following array to print out a list of Canadian states, also know as provinces.

philipnorton42 Thu, 04/23/2009 - 12:42
PHP Array Of USA States

Use the following array if you want to print out a list of USA states either as a list, or as a select box.

philipnorton42 Thu, 04/23/2009 - 12:32

Delete Trailing Commas In PHP

14th April 2009

Converting an array of information into a string is easy, but when you are doing this for insertion into a database having trailing commas is going to mess up your SQL statements.

Take the following example, which takes an array of values and converts them into a string of values. This practice is quite common in PHP database manipulation.

PHP Array Of UK Counties

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

philipnorton42 Wed, 03/04/2009 - 16:14