Skip to content

Instantly share code, notes, and snippets.

@piercemoore
piercemoore / arrayToObject.php
Created July 13, 2012 22:02
Convert a multi-dimensional array into an object
<?php
// Full disclosure: I found this solution here: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass and implemented it both here and in my project.
function arrayToObject( $array ) {
if ( !is_array( $array ) && !is_object( $array ) ) {
return $array;
}
$obj = new stdClass();
if( ( is_array( $array ) || is_object( $array ) ) && count( $array ) > 0 ) {
foreach( $array as $k=>$v ) {
@piercemoore
piercemoore / redditReaderBasic.php
Created July 13, 2012 16:55
Quick Reddit reader in PHP
<?php
// Full disclosure: I wrote this while writing a Twitter Bootstrap enabled site, so the class tags are Twitter bootstrap enabled. Shouldn't be hard to style, though.
$subReddit = "webdev"; // Enter the subreddit name here with no /r/ in front
$selfTextLimit = 250; // Character limit for self text entries
$pageData = json_decode( file_get_contents( "http://www.reddit.com/r/$subReddit/.json" ) );
foreach( $pageData->data->children as $post ): ?>
@piercemoore
piercemoore / gist:2292935
Created April 3, 2012 15:29
Simple Recursion Function within a Class
<?php
class Foo {
function __construct( $array ) {
$this->storage = $this->store($array);
}
function store( $array ) {
@piercemoore
piercemoore / gist:2253239
Created March 30, 2012 17:37
Handling unlimited/variable function arguments
function showAllArgs( $item1, $item2, $item3, etc...) {
$i = 0;
foreach( func_get_args() as $key => $val ) {
// $val is the value that you supplied to the parameter.
print "You supplied '$val' for argument #$i. \n";
$i++;
}
}
@piercemoore
piercemoore / placefooter.jquery.js
Created March 4, 2012 00:56
jQuery Function to Place Footer at the bottom of the page
<script type="text/javascript">
function placeFooter() {
if( $(document.body).height() < $(window).height() ) {
$("#footer").css({position: "absolute", bottom:"0px"});
} else {
$("#footer").css({position: ""});
}
}
@piercemoore
piercemoore / ajaxdetect.php
Created March 4, 2012 00:46
Super Simple AJAX Detection in PHP
<?php
/*
* isAjax determines whether a server request is a standard browser-based request or if it is being loaded via AJAX.
*
* REMEMBER: Do NOT use this for security purposes as it is VERY easy to spoof headers.
*
* @return bool
*/
function isAjax() {