array

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.

Conway's Game Of Life In PHP

12th April 2020 - 9 minutes read time

I was saddened to hear of the passing of the Mathematician John Horton Conway today so I decided to put together a post on his 'game of life' simulator.

This game of life, more commonly called Conway's game of life, was devised by John Conway in 1970 and is a way of modelling very simple cell population dynamics. The game takes place on a two dimensional board containing a grid of orthogonal cells. The game is technically a zero player game in that the initial setup of the game dictates the eventual evolution of the board.

The rules of the game (taken from wikipedia) are as follows.

Sieve of Eratosthenes In PHP

5th July 2013 - 4 minutes read time

The sieve of Eratosthenes is named after Eratosthenes of Cyrene who was a Greek mathematician who devised a mechanism to find a sequence of prime numbers using a simple algorithm.

Normally, looping through a list of numbers and finding the primes can be an expensive process. The seive of Eratosthenes is one of the most efficient way of working out all of the smaller prime numbers below (below 10 million or so).

The sieve works by looping through a list of consecutive numbers, starting at 2. For each number in the sequence the multiples of that number are marked to be removed from the list of numbers. When finished the numbers that are not marked are prime numbers.

This algorithm is pretty straightforward, but from that it is possible to create a simple PHP function that will generate all the prime numbers up to a given number.

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 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.

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.

Shuffle An Array In PHP

24th January 2008 - 3 minutes read time

To randomise an array in PHP use the shuffle() function like this.

  1. $array = range(1, 5);
  2. shuffle($array); // randomise array

There are two limitations to this function. The first is that it completely removes any key association that you may have set up. So the following array.