Detecting a Multi-Dimensional Array in PHP
I recently had to find a quick way to check if an array is a multi-dimensional array, only to find that there are no PHP built-in function to do this.
Here is a quick and easy way to do it:
1 2 function is_multi_array( $arr )3 {4 return is_array($arr[$key($arr)]);5 }6
This can easily be used in the following context:
1 2 $a = array("dimension1" => array("dimension2" => "I like it") );3 if( is_multi_array( $a )4 {5 echo $a['dimension1']['dimension2'];6 }7
Droid X Keyboard running on your HTC Evo 4G
The Droid X keyboard has been ripped from the phone, and can be downloaded and installed on your Evo (or any other Android 2.1 device)
It also appears that this will work on both rooted and non-rooted phones.
1. Grab the file here and put it on your SD Card
2. Once on the SD Card, tap on in, can tap “Install” *Note, your phone must be switched to install apps from “Unknown Sources”*
3. After installation, go to Settings > Language and Keyboard
4. Check the “Multi-touch Keyboard” box.
5. Go to any app with a textbox and long-press on the textbox.
6. Choose, “input method”
7. Select “Multi-touch Keyboard”
Let me know if this works on your Android phone.
How to make your Sprint Evo 4G last longer…
The Sprint EVO 4G, although and awesome phone, has a huge problem. The battery life sucks. The 1Ghz snapdragon, 4.3″ display, and a 4G connection is not doing the phone good when it comes to the battery.
Sprint released tips for increasing the battery life, but these are basic tips, and does not help much.
Here are a few more advanced tips.
- Turn off widget animations
Animations mean more CPU cycles, which means more battery life. Turn off all widget and screen animations.
How-to: Tap “Menu”, “Settings”, “Sound & Display”. Scroll to the bottom, look for “Animation”, tap it, and select “No Animations”. - Don’t use a live background
Having a live background is pretty awesome, but since it is constantly moving, it requires CPU cycles. - Decrease your screen brightness
Powering this monster of a screen is battery heavy, especially if you use it at its brightest setting. Switch off the “auto brightness” setting, and set it to something that you feel comfortable with.
How-to: Tap “Menu”, “Settings”, “Sound & Display”. Scroll to the bottom, look for “Brightness”, tap it, and select a level that is good for you. - Disable roaming
The phone is set to look for a GSM signal by default. This is a little strange considering the phone is running on a CDMA network. Switch it to a CDMA network only.
How-to: Tap “Menu”, “Settings”, “Wireless & Networks”. Scroll to the bottom, look for “Mobile Networks”, tap it, and tap on “Roaming”, then select “Sprint only”. - Don’t use widgets you do not need
Widgets a “live” applications. They constantly connect to the web to download data, or run little tasks on your home screen. Having those cool widgets look great on your screen, but can kill your battery quicker. - Eliminate bad applications
Some developers have bad practices and use bad code. Bad code means that the an application might need extra processing, which results in extra CPU cycles.
Some applications also prevent your phone from going into sleep mode, so it would be good practice to keep an eye on this.
You can keep an eye on applications by going to “Menu”, “Settings”, “About phone”, “Battery”, “Battery Use” - Switch off Wifi when you don’t use it.
Wifi is a big battery killer. Configure your phone to disable the Wifi after being inactive for a certain amount of time.
How-to: Tap “Menu”, “Settings”, “Wireless & Networks”. Look for “Wifi Settings”, tap it. Tap on “Menu”, then select “Advanced”. Look for “Wifi Sleep Policy” and tap on it. Change the setting to your preference.
Getting the name of a variable, as a string, in PHP
While working on a debug function for php I realized that there is no core function to display the name of a variable. I searched Google and could not find anything that is up to date with PHP 5.3, that’s when I realized that I could probably make something work with the get_defined_vars() function. I have to admit that this is probably not the most efficient way of getting the name of a variable, but it does the job. And since this will only be used in a development environment I don’t have to stress about too much memory usage.
Here is how it works:
1 2 $var_1 = 'something';3 echo get_variable_name( $var_1, get_defined_vars() ) . '<br />';4 5 $var_2 = array( 'hello', 'bye' );6 echo get_variable_name( $var_2, get_defined_vars() ) . '<br />';;7
These two function calls will echo out the variable names, var_1 and var_2 respectively.
Unfortunately I can not get this to work with the get_defined_vars() function inside of the get_variable_name() function, so the get_defined_vars() must be used as an argument in the function call. I suspect that this is because of PHPs bizarre way of using variables. PHP will pass a variable by reference, but as soon as you use the variable, a copy of it will be made, local to the scope of the function.
The true magic happens inside of this function:
01 02 function get_variable_name(&$var, $scope=null)03 {04 $vals = $scope;05 $old = $var;06 $var = $new = 'random'.rand().'value';07 $var_name = false;08 foreach($vals as $key => $val)09 {10 if($val === $new) $var_name = $key;11 }12 $var = $old;13 return $var_name;14 }15
To explain the function:
The variable is passed by reference. This must be done in order to get a pointer to the variable, and not the variable itself.
The scope is the scope within which you want to search for the variable. In this case, get_defined_vars() is used, which will return EVERY variable defined in the script.
A copy of the pointer to the reference variable is made.
Then a new, random, value is assigned to the variable in question.
PHP will then loop through the set of defined variables until it matches the random value.
If it does, it will assign the key of the array (variable name), to be returned.
Lastly, the old value is assigned back to the variable.
This works great for all types of variables, including class objects.
Making Your Radio Group Tab-able Using jQuery
Recently I had to come up with a solution to make radio boxes tab-able. Mozilla Firefox allows you to tab over each radio box in a group, however, all other browsers do not. The solution is actually quite simple.
First, in your HTML make sure to name your radio boxes with the same name, but also add a .1, .2, .3, and so on, to separate them. You will also need a hidden element. This is the one you want to do validation against.
For example:
1 2 <input type="hidden" name="group1" value="" />3 <input type="radio" name="group1.1" value="1" tabindex="10" /> Radio 14 <input type="radio" name="group1.2" value="2" tabindex="11" /> Radio 25 <input type="radio" name="group1.3" value="3" tabindex="12" /> Radio 36
Now add a little bit of code that will parse once the DOM is ready:
1 2 <script type="text/javascript">3 $(document).ready( function() {4 rt = new RadioTabs();5 rt.init( "group1", ["group1.1", "group1.2", "group1.3"], ["click"] );6 });7 </script>8
All this does is tell the RadioTabs class (coming next) which tabs will be in the group.
Finally, add this somewhere on you page. Preferably on a separate page.
01 02 /* Create Tabable Radio Boxes */03 function RadioTabs() {04 /* Declare Private Members and Methods */05 var hidden = '';06 var group = [];07 var events = [];08 09 /* void Bind Events to each individual Radio Box */10 var bindEvents = function () {11 //Loop through Events12 for( var e = 0; e < events.length; e++ ) {13 //Loop through Radio Buttons14 for( var g = 0; g < group.length; g++ ) {15 //Add a Rel value to each group box - to be used in the bind16 $('input:radio[name="' + group[g] + '"]').attr('rel', g);17 //Bind actual events to each radio box18 $('input:radio[name="' + group[g] + '"]').bind(events[e], function( event ) {19 var n = $(this).attr('rel');20 $('[name="' + hidden + '"]').val( $('[name=' + group[n] + ']').val() );21 //Step through radio group and set to false22 for( var i = 0; i < group.length; i++ ) {23 if( group[i] != group[n] ) {24 $('input:radio[name="' + group[i] + '"]:checked').attr( 'checked', false );25 }26 };27 });28 }29 }30 };31 32 /* Declare Public Members and Methods */33 return {34 /* string Set the Hidden Input Element */35 setHidden : function ( value ) {36 hidden = value;37 },38 /* array Set the Radio Group Elements */39 setGroup : function ( values ) {40 group = values;41 },42 /* array Set the Events to bind to */43 setEvents : function ( values ) {44 events = values;45 },46 /* void Init the script */47 init : function ( hid, grp, evt ) {48 this.setHidden( hid );49 this.setGroup( grp );50 this.setEvents( evt );51 bindEvents();52 }53 54 };55 };56
All the above piece of code does is it will allow you to tab through the radio boxes, and then set the hidden elements value to that of the selected radio box.
You might have to tweak a little bit with CSS in IE7 and 8 (I don’t support 6 anymore) to make it show that the box is selected.
This should do the trick
1 2 input[type=radio]:focus {3 outline:#000 dotted thin;4 }5
Moving the Ubuntu Window Buttons to the Right
With Ubuntu 10.04 the window buttons moved to the top left of the window. Why? Ubuntu is trying to convert MacOS X users. Its frustrating though, and I keep missing my window buttons on the right. Luckily Gnome made it easy to move these buttons.
In Ubuntu, hit ALT+F2, then type gconf-editor and click run. The “Configuration Editor” will open up. Click on Apps>Metacity>General and look for button_layout. I would suggest to change the configuration to :minimize,maximize,close. The colon (:) tells it to move the buttons to the right, with the buttons then in the defined order. You will see the buttons move when you close the Configuration Editor.
Ubuntu 10.04 with ACPI issue on my Toshiba L505 laptop.
So, you downloaded your free copy of Ubuntu 10.04 today, quickly burnt it to CD and booted it up, right? In my case, things did not go as smooth. I have a Toshiba L505 GS6000 laptop, with a 64bit AMD Triton II Dual Core processor. I started Ubuntu only to be greeted with tons of error output. It turns out that ACPI is causing issues.
Here is something you can try. At the Ubuntu start menu, on the Live CD, hit F6 and select ACPI=OFF Now start the installation. I managed to Install my favorite flavor of Linux this way. After installation, I had to start the Live CD, hit F6, switch off ACPI and start the Live CD. After the Live CD is started, mount your drive, simply go to Places and select the drive you installed Ubuntu on. Now, go to the terminal, type sudo -s and then type gedit. Gedit will now be running as a Super User. Click on File > Open. Navigate to your Ubuntu installation drive, open /boot/grub and click on grub.cfg
DO NOT MAKE ANY CHANGES to this file, except for this one line. (It might look a little different depending on your installation) Around line 70, you will see this: linux /boot/vmlinuz-2.6.32-22-generic root=UUID=403d86ce-f768-449e-88f1-ff9eb873fc5f ro splash quiet splash
You can simply switch acpi off, but I noticed that when I add acpi=off my laptop is only running on one CPU core, however, when I switch it to acpi=ht my laptop will run with both cores enabled. So change the above line to look something like this:
linux /boot/vmlinuz-2.6.32-22-generic root=UUID=403d86ce-f768-449e-88f1-ff9eb873fc5f ro acpi=ht splash quiet splash
This should take care of your problem, have your computer running with both processors, and resolve some WiFi issues.
