protected function format($data) { if (is_array($data)) { reset($data); if (is_numeric(key($data))) { return '[' . implode(', ', $data) . ']'; } $out = '{' . $this->newline; foreach ($data as $key => $val) { $elems[] = $this->indent . $this->indent . JsonFile::encode($key) . ': ' . $this->format($val); } return $out . implode(',' . $this->newline, $elems) . $this->newline . $this->indent . '}'; } return JsonFile::encode($data); }
public function format($data, $depth = 0) { if (is_array($data)) { reset($data); if (is_numeric(key($data))) { foreach ($data as $key => $val) { $data[$key] = $this->format($val, $depth + 1); } return '[' . implode(', ', $data) . ']'; } $out = '{' . $this->newline; $elems = array(); foreach ($data as $key => $val) { $elems[] = str_repeat($this->indent, $depth + 2) . JsonFile::encode($key) . ': ' . $this->format($val, $depth + 1); } return $out . implode(',' . $this->newline, $elems) . $this->newline . str_repeat($this->indent, $depth + 1) . '}'; } return JsonFile::encode($data); }