Archive for September 2006


A Single-function JSON Freakshow

September 23rd, 2006 — 8:22am

Want to see something scary and hacky? Check this out – it takes in a PHP array and makes a JSON message out of it:

function build($arr,&$str,$lvl=0,$in_arr=''){
	$lvl++;
	foreach($arr as $key => $value){
		if(is_array($value)){
			$str.=(($in_arr==1)?'{':'').'"'.$key.'": ';;

			if(count($value)>1){ $str.='['; 
			}elseif(count($value)==1){ $str.=''; 
			}else{ $str.='{'; }

			build($value,$str,$lvl,1);

			if(count($value)>1){ $str.=']'; 
			}elseif(count($value)==1){ $str.=''; 
			}else{ $str.='}'; }

			$str.=($in_arr==1) ? '}' : '';
			$str.=(next($arr)) ? ',' : '';
		}else{
			$str.=($lvl>1) ? '{' : '';
			$str.='"'.$key.'":"'.$value.'"';
			$str.=($lvl>1) ? '}' : '';
			$str.=(next($arr)) ? ',' : '';
		}
	}
}

And, of course, a sample of how to use it:

$my_data=array(
	"foo"=>"1", 
	"bar"=>"2",
	"baz"=>array(
		"one"=>"1",
		"two"=>"2"
	),
	"test"=>array(
		"my_test"=>array(
			"one_more"=>"1","three_more"=>"test"
		),
		"your_test"=>array(
			"two_more"=>"2"
		),
		"nest1"=>array(
			"nest2"=>array(
				"nest"=>"test"
			)
		)
	),
	"meedle"=>"squeedle"
);

$str='';
build($my_data,$str);
echo '{'.$str.'}';

Be afraid. Be very afraid :)

9 comments » | PHP

My New Addiction

September 21st, 2006 — 9:03am

My wife and I visited England (up by York and Manchester) and much fun was had by all.

Too bad I came back with an addiction. It’s taken hold of me and is constantly with me. I can’t stop thinking about it and I wonder when I can get my next fix. I feel the constant need to spend lots of money to feed my habit (and with the exchange rate right now, that’s not a good thing), and I just can’t get the stuff I need over here.

That’s right, I’m an addict.
I’m addicted to Tom Holt books (not quite what you were expecting, eh?)

Yes, I casually picked up one of his books (“Falling Sideways“) in a shop while we were over there and finished it in about two days. I have to say, I haven’t read anything so delightfully light-hearted and funny in a long time (we’ll, that wasn’t something I’d read a million times over).

Alas, my probem still remains – the Amazon.com US store doesn’t carry them all (only like 10 or so) and ordering them from overseas is a bit expensive right now for us U.S-ers. I’ve ordered “Omnibus 5: Tall Stories” and “The Portable Door” from Amazon, and I’m counting the days.

It’s always fun to discover a writer you really enjoy, especially when you find out that that writer has loads of books for you to catch up on :)

4 comments » | Personal

Here’s to the unsung programmers….

September 10th, 2006 — 5:38pm

A community is not about the members that get the most noteriety.

A community is not about the programmers that do the cool things or the bleeding edge stuff.

A community is not a select few.

A community is a group of people, centered around a cause, looking to do the best that they can to belong and to find thier place in the rankings that the community imposes.

See, the PHP community is like any other. There are those out there that are the notables, the ones that get mentioned in press releases or in lots of news postings (yes, guilty, I know). But for each of the ones that gets creadit for the advancements in the languge, there are tens or hundreds more behind them pushing and helpong to get things done.

Here’s to the unsung heroes of the PHP community – those developers that remain nameless, but contribute their 110% to the language or to developing their applications. They work thing fingers to the bone (well, okay, so maybe not quite that much) trying to further the cause of PHP. There are countless developers looking to forge their own path through the PHP wilderness without getting the recongition they deserve.

Yes, I’m guilty of focusing on a “chosen few” that have proven themselves in the past and have made a name for themselves in the community, but there are tons more out there, woorking on their own projects, making their own ways that aren’t acknowledged and that aren’t brought into the public spectrum.

So, here’s to you the unsung programmers out there – thank you for all that you do to help the PHP community become what it is.

If you’re at the ZendCon 2006 this year, find me and I’ll buy you a drink.

2 comments » | PHP

Back to top