PHP: Merge Two Arrays but Keep Their Keys Intact

Combining two or more arrays whilst keeping their keys intact in PHP is deceptively simple – though not if you assume you need to use a function like array_merge to achieve this!

As you scroll through the various array functions available to you, you will notice that quite a few seem to drop the array keys after they are done, which is pretty useless if all you want to do is basically add two arrays to one another to form one big keyed array.

So how do you do this then?

Simple really. Just literally add them together!

$resultarr = $array1 + $array2;

And just in case you are wondering what the hell is going on, remember that when applied to arrays, the + acts as a union operator.

Nice!

Posted in Tutorials | Tagged , , , , | View Comments

How Ubuntu’s Version Numbering Works

So the latest codename for next year’s first planned Ubuntu release has been revealed as being Natty Narwhal, not perhaps the most awe-inspiring of codenames but one I guess which will work.

Still, I kind of like the fact that the next one coming up is nicknamed “Maverick Meerkat” – just the word Meerkat makes me feel all proudly South African for some strange reason.

Anyway, the point of this quickfire post is to explain the version numbering which Canonical employs for its Ubuntu releases.

As we all know by now, Ubuntu is released on a time-based six-month release cycle. So that’s two versions a year, which typically arrive in April and October respectively.

The codename for each new release is based on an advancing alphabetically ordered sequence, and consists of an animal name preceded by an adjective that starts with the same letter. This then explains why we’ve had things like Intrepid Ibis, Jaunty Jackalope, Karmic Koala and Lucid Lynx, and getting things like Maverick Meerkat and Natty Narwhal. An entertaining system for sure, but one which will have to be adjusted once they hit Z I’m sure! :)

The actual version number is based on the year and approximate month of the planned release date. Hence Ubuntu 10.04 refers to the April 2010 release.

And that’s pretty much that. Sure, sometimes they go and throw on little extra like LTS after the version number, but all that this means is that the OS release qualifies for Long Term Support, which basic means Canonical will support it for at least three years.

Simple, and if you didn’t before, now you know! ;)

Posted in Software & Websites | Tagged , , , , | View Comments

How to Install Webmin on Ubuntu 10.04 Lucid Lynx Server

Installing Webmin in Ubuntu is not exactly a quick win thanks to Webmin’s reliance on a deprecated PERL package (an MD5 wrapper to be exact) that Ubuntu and the like just don’t want to include any more, but never fear, following these steps one by one will have you up and running in absolutely no time!

Install the dependencies:

sudo aptitude -y install perl libnet-ssleay-perl openssl libauthen-pam-perl libpam-runtime libio-pty-perl libmd5-perl apt-show-versions libapt-pkg-perl

While most of the packages above will all successfully install, you should get an error message that reads something like this:

Couldn’t find any package whose name or description matched “libmd5-perl”

The next step is then obviously to manually grab it and install it.

Run the following:

wget http://ftp.debian.org/pool/main/libm/libmd5-perl/libmd5-perl_2.03-1_all.deb

followed by

sudo dpkg -i libmd5-perl_2.03-1_all.deb

All right, all set. Go to webmin’s home page and grab the download link to the latest version. In my case, this was thrown up:

wget http://downloads.sourceforge.net/project/webadmin/webmin/1.510/webmin_1.510-2_all.deb?use_mirror=cdnetworks-us-1

Download it and once done, run the final install:

sudo dpkg -i webmin_1.510-2_all.deb

All done. You should now be able to run the webmin web manager from another PC by hitting http://10.0.0.6:10000/ (using your server’s IP address of course!)

Nifty.

Posted in Software & Websites, Tutorials | Tagged , , , | View Comments

A Quick Guide to Bash Shell Scripting

I stumbled across this gem of a quick guide to Bash shell scripting over at some or other forum and so as not to lose this valuable resource for my later use, I’ve decided to duplicate it here and thus save it for prosperity.

Common environment variables

PATH – Sets the search path for any executable command. Similar to the PATH variable in MSDOS.

HOME – Home directory of the user.

MAIL – Contains the path to the location where mail addressed to the user is stored.

IFS – Contains a string of characters which are used as word seperators in the command line. The string normally consists of the space, tab and the newline characters. To see them you will have to do an octal dump as follows:

$ echo $IFS | od -bc

PS1 and PS2 – Primary and secondary prompts in bash. PS1 is set to $ by default and PS2 is set to ‘>’ . To see the secondary prompt, just run the command :

$ ls |

… and press enter.

USER – User login name.

TERM – indicates the terminal type being used. This should be set correctly for editors like Vim to work correctly.

SHELL – Determines the type of shell that the user sees on logging in.

Note: To see what are the values held by the above environment variables, just do an echo of the name of the variable preceeded with a $. For example, if I do the following:

$ echo $USER
ravi

… I get the value stored in the environment variable USER.

Continue reading

Posted in Tutorials | Tagged , , , , | View Comments

jQuery: Invert a Selection of Checked Checkboxes

If you have a group of checkboxes on a page, sometimes it is quite nice to give the user some quickfire controls that allows him to select all or select none at a click of a button. Similarly, it is pretty cool to give him the option of inverting his current selection, in other words checking all of those checkboxes currently unchecked, while unchecking all the currently ticked checkboxes.

To do this is fairly simple.

First, we built up a jQuery object containing all checked checkboxes. Then we repeat this for all unchecked checkboxes. Next, using the incredibly useful .each() function, we iterate over both collections and either set or unset the checked attribute accordingly.

In code, your invert function would look like this:

$('#createrightstoggle').click(function(){
    var checked = $('.createrightcheckbox').filter(":checked");
    var unchecked = $('.createrightcheckbox').filter(":not(:checked)");
    checked.each(function(){$(this).attr('checked',false);});
    unchecked.each(function(){$(this).attr('checked',true);});
    createrightscounterupdate();
});

Nifty.

Posted in Technology & Code, Tutorials | Tagged , , , , | View Comments