PHP Arrays

Posts about arrays in PHP

PHP Logo

PHP: Find An Array Sequence In An Array

6th March 2021 - 3 minutes read time

I was looking for a function that searched an array for another array, and after not finding one I decided to write it. What I was looking for was a function that took a smaller array and searched for that exact sequence of items in a larger array. As it happens, PHP does have a number of array search functions, but they didn't produce the correct result.

The array_search() function does accept an array as the needle to be searched for, but this does a multi-dimensional search instead. I also saw some techniques using array_intersect() or array_diff(), and although these functions were able to find one array inside another I was interested in the sequence. 

The implementation of the function contains a two loops, one for going through the haystack array and one for the needle array. If the number of matches found equals the number of items in the needle array then the function returns true.

Here is the sequence_in_array() function.

PHP Logo

Bogo Sort In PHP

23rd June 2018 - 4 minutes read time

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.

  1. Take an array.
  2. Shuffle it.
  3. If the array sorted then stop.
  4. If the array is not sorted then return to step 2.

As you can see, this isn't so much a sorting algorithm, more of a game of chance. Due to the random nature of the sort you might need to wait a very long time before the array is actually sorted.

Here is an implementation of a bogo sort in PHP.

PHP Logo

An Implementation Of Array Binary Search In PHP

19th June 2018 - 4 minutes read time

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.

PHP Logo

Finding The First And Last Items In An Array In PHP

24th March 2013 - 6 minutes read time

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.

PHP Logo

Using list() With explode() In PHP

8th September 2009 - 2 minutes read time

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 Logo

Print Array Without Trailing Commas In PHP

24th April 2009 - 2 minutes read time

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.

PHP Logo

PHP Array Of Canadian States

23rd April 2009 - 1 minute read time

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

PHP Logo

PHP Array Of USA States

23rd April 2009 - 1 minute read time

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

PHP Logo

Delete Trailing Commas In PHP

14th April 2009 - 2 minutes read time

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.