All pastes #971588 Raw Edit

recusrive JSON generating functi

public php v1 · immutable
·published 2008-04-04 21:11 UTC
rendered paste body
    function exportToJSON($root=null, $tabs="\t") {        $json = null;        $first = false;        if (is_null($root)) {            $root = $this->top_element;            $first = true;        }                $type = 'undefined';        if (strpos($root['object']->table, 'grade_categories') !== false) {            $type = 'category';        } elseif (strpos($root['object']->table, 'grade_items') !== false) {            $type = 'item';        } elseif (strpos($root['object']->table, 'grade_outcomes') !== false) {            $type = 'outcome';        }                $json .= "$tabs {\n";        $json .= "$tabs\t \"type\": \"$type\",\n";        foreach ($root['object'] as $var => $value) {            if (!is_object($value) && !is_array($value) && !empty($value)) {                $json .= "$tabs\t \"$var\": \"$value\",\n";            }        }                $json = substr($json, 0, strrpos($json, ','));        if (!empty($root['children'])) {            $json .= ",\n$tabs\t\"children\": [\n";            foreach ($root['children'] as $sortorder => $child) {                $json .= $this->exportToJSON($child, $tabs."\t\t");            }            $json = substr($json, 0, strrpos($json, ','));            $json .= "\n$tabs\t]\n";        }         if ($first) {            $json .= "\n}";        } else {            $json .= "\n$tabs},\n";        }                return $json;    }