Convert POST/GET queries to object in JS

A handy JS string prototype function for converting POST/GET queries to objects.

String.prototype.toURLobject = function() 
{
	var query = decodeURIComponent(this);
	var parts = query.split("&");
	var obj = {};
	for(var i = 0; i < parts.length; i++)
	{
		var pair = parts[i].split("=");
		var index = pair[0].indexOf("[]");
		if (index != -1)
		{
			var name = pair[0].substr(0, index);
			if (!obj.hasOwnProperty(name))
			{
				obj[name] = [];
				obj[name].push(pair[1]);
			}
			else
				obj[name].push(pair[1]);
		}
		else
			obj[pair[0]] = pair[1];
	}

	return obj;
}

in_associative_array PHP function

Pretty straightforward function for determining if associative array already contains value in specified array column.

function in_associative_array($needle, $haystack, $column, $nivo = 1)
{
	/*
		$haystack = array(
			[0] = array(
				"column1" => "value1",
				"column2" => "value2"
			),
			[1] = array(
				"column1" => "value1",
				"column2" => "value2"
			)	
		)	
	*/
	
	if ($nivo == 1)
	foreach($haystack as $item)
	{
		if (isset($item[ $column ]) && $item[ $column ] == $needle)
			return true;
	}
	
	/*
		$haystack = array(
			"column1" => "value1",
			"column2" => "value2",
			"column3" => "value1",
		)	
	*/	
	if ($nivo == 0)
	foreach($haystack as $key => $item)
	{
		if ($key == $column && $item == $needle)
			return true;
	}	
		
	
	return false;
}

Phaser Sprite.destroy()

I’ve been messing around with Phaser lately and it turned out to be an amazing game development library. I was always keen on using my on tech on everything I worked as it seemed easier to manage (“i know where everything is and what everything does” argument and similar nonsense) but Phaser really stands out for it’s simplicity, and what it offers. For a price of 0!

Anyway, while I mostly embraced Phaser’s concepts I cannot agree with the recycling sprites theory, telling me to kill() sprites and reuse them. So I should create multiple objects, add them to groups and then call kill() method on them when I don’t need them anymore and reuse them later (reset() method in conjunction with getFirstDead() method). While this sounds pretty straightforward and somewhat logic I disagree with it.

I am using multiple objects of same “class” but with various types (let’s say red, blue and yellow cars) and if I use kill() method on them I still need to cycle through all dead cars and get first red car if I wanna reuse red car. This seems kind of overkill – why would I wanna do that if I can simply create new red car and be done with it, not worrying about if I have any red car to in a pool of dead red cars to reuse.

So I simply went and used destroy() on the cars I wanted to destroy (let’s say in a game.physics.arcade.overlap() callbacks) and got various undefined errors, because of the way loops through objects in Phaser happen. When I want to destroy objects from various arrays and list I go from the last to first index and remove stuff designated to be destroyed, not fearing I’ll run out of bounds (removing objects from arrays while going from 0 index to size – 1 will quickly produce out of bounds errors), but Phaser loops (for overlaps, rendering, updates, etc.) work from index 0 to last, which is logical, the way they are meant to. So if I remove something from object arrays on the run stuff like out of bounds indexes and similar occur relatively quickly.

So in order to DESTROY something from object groups I called the kill() method on regular update loops and then called the cleanUp() method at the end of each update loop, like this:

	
function cleanUp()
{
	var aCleanup = [];
	cars.forEachDead(function(item){
		aCleanup.push(item);
	});
	
	var i = aCleanup.length - 1;
	while(i > -1)
	{
		var getitem = aCleanup[i];
		getitem.destroy();
		i--;
	}
}

Here I iterate through all the cars I’ve set as dead in regular update functions, save them into cleanup array and then go through it and call destroy() method on every car set to be removed, without getting any errors.

Prettier, customizable print_r function

When developing random stuff with PHP I tend to rely heavily on various variable printing so I made a small function that I use to print variables the way I want to.

<?php
	function print_pre($text, $ret = false, $inStyles = array()) {
	$aStyles = array("-webkit-border-radius" => "3px", 
		"-moz-border-radius" => "3px", 
		"border-radius" => "3px",
		"background-color" => "#F0F0F0", 
		"padding" => "3px",
		"border" => "1px solid #A8A8A8");
	
	if (is_array($inStyles))
	{
		foreach($inStyles as $key => $value)
			$aStyles[$key] = $value;
	}
		
	$applyStyles = array();	
	foreach($aStyles as $key => $value)
		array_push($applyStyles, $key . ": " . $value . ";");

	$return = "<pre style='".implode(" ", $applyStyles)."'>" . print_r($text, true) . "</pre>";
	if($ret) return $return;
	print($return);
}
?>

It’s pretty straightforward – it takes 3 arguments, 1st being variable we want to display, 2nd if we want the return string returned or directly printed on page and finally CSS styling pairs as associative array.

Few examples of use

Default print

print_pre("Test string", true);

Result:

Test string

Custom style

print_pre("Test string", false, array("background-color" => "#000", "color" => "#fff"));

Result:

Test string

Printing an object

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$object = json_decode(json_encode($array), FALSE);
print_pre($object, false, array("background-color" => "blue", "color" => "#fff"));

Result:

stdClass Object
(
    [name] => david
    [age] => 30
    [gender] => male
)

Getting the return print string of regular array and echoing it as a variable

$array = array("name" => "david", "age" => (date("Y") - 1987), "gender" => "male");
$value = print_pre($array, true, array("background-color" => "yellow", "color" => "gray"));
echo $value;

Result:

Array
(
    [name] => david
    [age] => 30
    [gender] => male
)