Tag: code

Using subkey to sort an array in PHP

It’s a bit tricky to sort multidimensional arrays in PHP by one of array’s subkeys, but it can absolutely be done. Check it out:

/**
 * USING POINTER TO ARRAY - sort an array of arrays with associative indeces by one of those associative values
 * @param $array pointer to our array
 * @param $subkey associative index that we want to order by
 * @param $sort_type [SORT_ASC|SORT_DESC]
 * @return void
 */
function sortBySubkey(&$array, $subkey, $sort_type = SORT_ASC) {
   foreach ($array as $subarray) {
      $keys[] = $subarray[$subkey];
   }
   array_multisort($keys, $sort_type, $array);
}

Creating absolutely safe strings

Sometimes you are given a string (someone’s name, or some title, or something completely different) and you cannot afford to use this string if you’re not completely sure what to expect. For instance, sometimes you need to convert a string into something that consists only of alphanumerical characters and spaces. You can always use the following function:

/**
 * create a string that only consists of alphanumerical characters and spaces
 * @param $string
 * @return string
 */
function createSafeString($string) {
	$string = strtolower($string);
        $output = "";
	for ($i = 0; $i < strlen($string); $i++) {
		$ord = ord($string[$i]);
		if (($ord >= 48 && $ord <= 57) || ($ord >= 65 && $ord <= 90) || ($ord >= 97 && $ord <= 122) || ($ord == 32)) {
			$output .= $string[$i];
		}
	}
	return $string;
}

Don’t manually escape all your variables

Let’s start with something fairly simple, shall we?

I’ve seen way too many programmers escaping all $_POST and $_GET variables like this:

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);

I suggest you simply use something like this from now on:

$post = array();
$get = array();
foreach ($_POST as $key => $value) {
	$post[$key] = escape_string($value);
}
foreach ($_GET as $key => $value) {
	$get[$key] = escape_string($value);
}
/**
 * escape given variable so we can use it in an SQL query
 * @param anything $value
 * @return anything $escaped_value
 */
function escape_string($value) {
	if (get_magic_quotes_gpc()) {
		$value = stripslashes($value);
	}	
	if (!is_numeric($value)) {
		$value = mysql_real_escape_string($value);
	}	
	return $value;
}

This way you can always use $post instead of $_POST and $get instead of $_GET if you need escaped values. And you can always access the original values in the original $_POST and $_GET variables. Pretty neat, huh? 🙂

Okay, I give up – here’s my blog!

As many of you already know, I’ve been more than active in the online business for the last couple of years (has it really been 4 years already?!), but I was always simply posting comments to existing blogs of our projects / products / services, commenting on marketing and web-dev forums, and I never had my own blog.

Over the time, there were so many fellow internet marketers asking me to start a blog to write down my thoughts and code tutorials etc. that I eventually had to give it a thought. And here it is. Aaron’s blog about internet business in general, business principles, inspiration, search engine optimization, marketing, revenue generation and LOTS of code.

I hope you like it! 😉