Total results: 11

Sort string array items as integer with PHP

When sorting array items in PHP, normally you can use one of the PHP sort functions such as sort() or usort(). This will sort your array into ascending order for either integer, starting with zero or string, from A – Z.

However, consider the following example:

$scores = array(
    '32-Andy',
    '43-Steve',
    '28-Joe'
);

If you try to sort these by the prefixed ID (28, 32, 48) the sort function will not take just the numbers into account as the criteria. It will also then sort on the string, from A to Z.

There’s a simple way to fix this. We need to augment the usort() function by adding an extra argument. We’ll add the SORT_NUMERIC predefined constant as the second argument and this will instruct PHP to ignore sorting on the string segment of the array values and instead successfully sort the array by the ID only.

Using the following code will output the array in the order: 2, 0, 1 as expected.

usort($scores, SORT_NUMERIC);
print_r($scores);

//0 = 28-Joe
//1 = 32-Andy
//2 = 43-Steve

FTP to a local wordpress install on OSX

If you have the problem when working on a local wordpress installation where you need to do an upgrade or install a plugin, you may be presented with the following screen asking for FTP details to the server.

Wordpress plugin install FTP prompt

Lets assume you need to install a plugin. One option would be to manually download the plugin and move it into the wordpress plugins directory, but this is frustrating and slow. The better way is to let wordpress handle everything for you.

Install LAMP server on Debian Wheezy from scratch

Debian Wheezy is the recommended UNIX package for a Raspberry Pi. After setting up your device and completing the raspi-config steps, simply run the following commands line-by-line in the bash terminal to create a functioning web server.

sudo apt-get install -y build-essential make
sudo apt-get install -y mysql-server mysql-client
sudo apt-get install -y apache2
sudo apt-get install -y php5 libapache2-mod-php5

The web server root can be found at /var/www/ and will contain a default index.html file. Type the IP address of the Raspberry Pi into a web browser and a page similar to this should be presented.

Apache default index

The root web directory is owned by the Apache user so to make changes within it write and execute rights must be given to the current user. Please note that this may compromise security, so not advised for a publicly accessible server.

sudo chmod a+wx /var/www/

Now files can be created and saved in the www directory. Create a new file called info.php and type the following into it:

 <?php phpinfo(); ?>

Again, open the Raspberry Pi IP address in a web browser but this time add /info.php to the URL. If everything has been installed correctly an information screen of the PHP environment will be displayed.

PHP Info Screen

To install Node.JS on Debian Wheezy, please see this article: Install Node.JS on Debian Wheezy

Show git branch and directory in your shell

It's really handy to be able to see the current git branch in your shell prompt along with the directory name. This for a start removes many "git branch" commands from your history.

To enable this feature, you have to edit your .bashrc file on Linux or the .bash_profile on Mac. These files usually exist in your home directory (The default directory when you open a shell) and are executed when the shell logs in. You can edit this file with any text editor such as Gedit or Textmate.

Add the following 2 lines after any existing SP1 variables and then save your file. Open a new shell to see the changes.

GIT_PS1_SHOWDIRTYSTATE=false
export PS1='\[\033[1;36m\]\u at\[\033[00m\] \[\033[34m\]\W\[\033[31m\]$("__git_ps1")\[\033[00m\]  >  '


SP1 colour example

Changing colours

If you would like to update the colours of the prompt, then visit this article to get a grasp of the basics and you can find much more on Google from there.

Sort string array values as integer with PHP

Sometimes you'll want to sort an array by its values. Normally you can use one of the PHP sort functions such as sort() or usort(). This will sort your array into ascending order for either integer, starting with zero or string, from A - Z.

However, you may have the following array:

 
$scores = array(
	'32-Andy',
	'43-Steve',
	'28-Joe'
);

If you try to sort these by the ID (28, 32, 48) the sort function will not take just the numbers into account. It will also then sort on the string, from A to Z.

There's a simple way to fix this. We need to upgrade the usort() function by adding an extra argument. We'll add the SORT_NUMERIC predefined constant as the second argument and magically PHP will ignore sorting on the string part of the array values and instead successfully sort the array by the ID.

Using the following code will output the array in the order: 2, 0, 1 as expected.

usort($scores, SORT_NUMERIC);
print_r($scores);
//0 = 28-Joe
//1 = 32-Andy
//2 = 43-Steve

PHP Installer doesn't ask for web server

If you are having the problem that the PHP Installer MSI is not asking for your web server type and therefore not updating your server configuration or not installing the correct libs such as "php5apache2_2.dll" then you are probably using the wrong MSI.

All of the MSI installers are on the http://windows.php.net/download/ page. You do not want to use the VC9 installers. You need to use the VC6 x86 Thread Safe installer. This is usually the last option, per version.

The link to the latest safe MSI is here: PHP 5.3 VC6 Thread Safe MSI

Fix "A connection attempt failed because the connected party did not" PHP error

I recently upgraded my PHP version to 5.3 and afterwards could not connect to MySQL. I was getting the following error in my PHP error log: PHP Warning: mysql_connect(): [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306)

The solution is to do with Windows enabling IPv6 support while MySQL does not support this yet. 'localhost' is being converted to ::1 instead of 127.0.0.1, the solution to this is simple.

Go the the following directory: C:\Windows\System32\drivers\etc

Allow yourself full access to the hosts file by right clicking it and clicking properties.

Next go to the security tab and click the big "Edit" button.

Click on the "Users" group. Below this check the "Full Control" box and then save the changes.

Now that you have access to the file, open the hosts file with a text editor such as notepad.

Comment out the ::1 line with a # so the file will look something like:

# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1       localhost
#::1             localhost

Once you are done, save the file and try to access your PHP page again that is using MySQL. It should work immedietley.

© Blake Simpson, 2012 – 2017