Tag Archives: reset

PHP: Get the first Key of an Associative Array Tips, Tricks and Tutorials 16 JUN 2018

I am not completely sure about it, but I somehow don’t think that I’ve ever needed to get the first key of an associative array using PHP before. Sure, a simple problem with a simple solution, but seeing as I needed to Google this one, I’m jotting down the answer here for future reference.

So, our example for the day then: Given the following array, print out the first key.

$months = array(
'jan'=>'January',
'feb'=>'February',
'mar'=>'March',
'apr'=>'April',
'may'=>'May',
'jun'=>'June',
'jul'=>'July',
'aug'=>'August',
'sep'=>'September',
'oct'=>'October',
'nov'=>'November',
'dec'=>'December'
);

reset($months);
$result = key($months);
if ($result !== NULL){
echo $result; //this will print 'jan'
}

The important bits are reset(), which moves the internal array pointer back to the first element of the array, and then key(), which retrieves the index element at the current array position (dictated by the internal pointer).

Simple (when you read the documentation).

UPDATE: As pointed out by one of my readers, if you know you have an array with at least one element in it, you could always just use array_keys($months)[0] as an alternative.

Related Link: reset() | key()

How to Hard Reset an iPod Nano CodeUnit 03 MAR 2011

I have a shiny red 4th generation 4GB iPod Nano (special HIV/AIDS edition) that was gifted to me on my parting from Commerce I.T. after 7 years of loyal service to them. It’s a great little MP3 and video player, perfect for the gym in fact.

So you can imagine my frustration in getting home after a long day at work, whipping out the iPod Nano from my gym bag in order to update its track listing, only to find it completely dead as a doornail.

Totally bricked.

Crap. Despondent, I lamented the issue of FaceBook and within a couple of minutes, my phone rang with my brother on the line. “Have you tried doing a hard reset yet?”

Eh? While not listed in the manual, there is in fact a way to reset your iPod Nano which should hopefully allow it to restore itself to working order and get out of its brick-like crash mode.

To perform the reset is remarkably simple once you know how. First, lock the iPod and then unlock the iPod in the usual fashion. Once unlocked, press and hold both the top menu button as well as the centre circle button together for about 15 seconds. At the end of this period, the Apple logo should show up on the screen.

A couple of seconds later, you will be back in action, ready to be entertained as if there was nothing ever wrong in the first place.

Nifty.

Ubuntu: Change or Reset the MySQL Root Password CodeUnit 04 NOV 2010

MySQL gets installed with a default root account under the username “root”. Sometimes the system will allow you to install a root account without a password (VERY not safe), but for the most part you have to set a password on install.

If of course you are anything like me, you instantly forget this password and a month down the line when you come back to do some more tinkering, you’ll quickly realize that you need to change the damn password – and this is one way you could go about doing it!

First, stop the MySQL server by entering the following into a terminal:

sudo /etc/init.d/mysql stop

Next, start a custom mysqld configuration by skipping the password tables:

sudo mysqld –skip-grant-tables &

The next step is then to login as root:

mysql -u root mysql

Finally, replace the existing password with your new one and Bob’s your uncle!

UPDATE user SET Password=PASSWORD(‘YOURNEWPASSWORD’) WHERE User=’root’; FLUSH PRIVILEGES; exit;

Done. Oh, and don’t forget to restart the service with sudo /etc/init.d/mysql start if necessary.

(Ubuntu 10.10 Maverick Meerkat)

PHP: Delete all Values to Reuse a Keyed Array CodeUnit 25 MAR 2010

It’s sometimes pretty valuable to reuse array structures if you’re kind of doing a task over and over again, and don’t necessarily want to recreate the array’s keyed structure from scratch with each iteration. In other words, it would be really nice to have a function that automatically sets every element in an array to null but doesn’t actually mess up the array in the process.

Sounds pretty logical and something that most people could use, but believe it or not, no native little PHP function actually exists to do this for us! O.O

Okay, so now that you are over that little bit of shock and horror, let’s quickly whip up some code to do the job ourselves. First, we’ll start be declaring a function that returns a blank or in this case empty string value:

function clearValue($value)
{
	$value = '';
	return $value;
}

Right, the next step is then to apply the handy array_map function we discussed previously in this blog, forcing it to apply our newly created function to the array at hand.

$qsvalues = array_map("clearValue",$qsvalues);

And Bob’s your uncle. The keyed array $qsvalues will now be sitting with a blank string in each and every one of its elements, exactly what we were looking for! :)