Location: PHPKode > scripts > Multi Dimensional Array > multi-dimensional-array/mda.php
<?

class mda {

    var $mda = Array();
    var $dim = 0;
    var $dms = 0;
    var $end = '0';
    
    function mda($dim, $end = 'END') {
        $this->dim = $dim;
        $this->end = $end;
        $this->dms = pow(2, $dim);
    }

    function add($lib, $add = 1) {
        for ($d = 0; $d < $this->dms; $d++) {
            $flt = str_pad(decbin($d), $this->dim, '0', STR_PAD_LEFT);

            for ($l = 0; $l < $this->dim; $l++) 
                $libs[$l] = substr($flt, $l, 1) == '1' ? $lib[$l] : $this->end;

            $di_ = $this->dim - 1;
            $mnk = &$this->mda;
            foreach ($libs as $key => $val) {
                if (empty($mnk)) {
                    if ($key < $di_) $ini = Array();
                    else $ini = 0;
                    $mnk = Array($val => $ini);
                }
                $mnk = &$mnk[$val];
            }
            $mnk += $add;
        }
    }

    function res() {
        return $this->mda;
    }
}

?>
Return current item: Multi Dimensional Array