PHP Arrays

Posts about arrays in PHP

Bogo Sort In PHP

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.

philipnorton42 Sat, 06/23/2018 - 20:42

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:

PHP Array Of USA States

23rd April 2009

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

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.