public function encode($d)
	{
		if (is_array($d)) {
			$def = 'd';
			$s = '';

			// If first array key is an integer, assume list.
			$list = false;
			if (is_int(array_keys($d)[0])) {
				$list = true;
				$def = 'l';
			}

			ksort($d, SORT_STRING);

			foreach ($d as $key => $value) {
				if (is_string($key)) {
					if ($list) {
						throw new Exception('Invalid bencode data.', 1);
					}
					$s .= strlen($key) . ':' . $key;
				}
				if (is_int($value) || is_float($value)) {
					$s .= 'i' . $value . 'e';
				} elseif (is_string($value)) {
					$s .= strlen($value) . ':' . $value;
				} else {
					$s .= $this->encode($value);
				}
			}

			return $def . $s . 'e';
		} elseif (is_int($d)) {
			return 'i' . $d . 'e';
		} elseif (is_string($d)) {
			return strlen($d) . ':' . $d;
		} else {
			throw new Exception('Invalid data provided to Bencode.', 1);
		}
	}