Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Keith Gaughan http://talideon.com/

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Flattening an array of arrays in PHP

It does just what it says on the tin:
/** Denests the nested arrays within the given array. */
function flatten_array(array $a) {
    $i = 0;
    while ($i < count($a)) {
        if (is_array($a[$i])) {
            array_splice($a, $i, 1, $a[$i]);
        } else {
            $i++;
        }
    }
    return $a;
}

With added unit-testy goodness:
class FlattenTest extends PHPUnit_Framework_TestCase {

    public function test_flatten() {
        $this->assertEquals(array(), flatten_array(array()));
        $this->assertEquals(array(1), flatten_array(array(1)));
        $this->assertEquals(array(1), flatten_array(array(array(1))));
        $this->assertEquals(array(1, 2), flatten_array(array(array(1, 2))));
        $this->assertEquals(array(1, 2), flatten_array(array(array(1), 2)));
        $this->assertEquals(array(1, 2), flatten_array(array(1, array(2))));
        $this->assertEquals(array(1, 2, 3), flatten_array(array(1, array(2), 3)));
        $this->assertEquals(array(1, 2, 3, 4), flatten_array(array(1, array(2, 3), 4)));
    }
}

Value coalescence in PHP

Here's something I keep on having to do in PHP and other languages. Often I need to find the first non-empty value from a set of arguments. Here's how I do it.
function coalesce() {
    $args = func_get_args();
    foreach ($args as $arg) {
        if (!empty($arg)) {
            return $arg;
        }
    }
    return $args[0];
}

The function assumes that it will be called with at least one value. It can be trivially altered to use is_null() instead if that suits.

An analogue of ColdFusion's CFPARAM tag in PHP.

/**
 * Checks if $var is already defined and if not, sets it to
 * $default.
 *
 * @param  $var         Variable to check.
 * @param  $default     Value to give the checked variable if
 *                      found to be null; "" by default.
 * @param  $catchNulls  TRUE: treat empty strings as null; FALSE
 *                      by default.
 *
 * @note   It's a lot like ColdFusion's <CFPARAM> tag.
 */
function set_default(&$var, $default="", $catchNulls=FALSE) {
    if (!isset($var) || ($catchNulls && $var == "")) {
        $var = $default;
    }
}

This appears to have stopped working as of PHP 5.

The world's simplest (decent) PHP templating engine...

...is PHP, so why not use it?
function include_view($__view, $vars=NULL) {
    # Start buffering the generated text.
    ob_start();

    # Process the view.
    if (!is_null($vars)) {
        extract($vars, EXTR_OVERWRITE | EXTR_REFS);
    }
    include("views/$__view.php");

    # Grab the generated content and clean up.
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS