@param $string
@param $assoc

@return array
@throws UndecodeableException
969358
private static function decodeArray($string, $assoc)
    {
        // Start empty.
        $arr = array();
        $size = 0;
        $offset = 1;

        // Get some information on the first byte.
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        if ($firstOctal === '9') {
            // fixarray 1000xxxx
            $size   = hexdec(substr($firstHex, 1));
            $offset = 1;
        } elseif ($firstHex === 'dc') {
            // arrar 16
            $size   = hexdec(unpack('H*', substr($string, 1, 2)));
            $offset = 3;
        } elseif ($firstHex === 'dd') {
            // array 32
            $size = hexdec(unpack('H*', substr($string, 1, 4)));
            $offset = 5;
        }

        // Shorten the string.
        $string = substr($string, $offset);

        // Loop to decode every item of the array.
        for ($i = 0; $i < $size; $i++) {
            list($item, $string) = self::decodeRecursive($string, $assoc);
            $arr[] = $item;
        }

        // Return the decoded data and the resulting string.
        return array($arr, $string);
    }
890205
public static function decodeRecursive($string, $assoc = false)
    {
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        switch (true) {
            case $firstOctal === '8': // fixmap 1000xxxx
            case $firstHex === 'de': // map 16
            case $firstHex === 'df': // map 32
                return self::decodeMap($string, $assoc);
            case $firstOctal === '9': // fixarray 1001xxxx
            case $firstHex === 'dc': // array 16
            case $firstHex === 'dd': // array 32
                return self::decodeArray($string, $assoc);
            case hexdec($firstHex) < pow(2, 7): // fixint 0xxxxxxx
            case hexdec($firstOctal) > 13: // negative fixint 111xxxxx
            case $firstHex === 'cc': // uint 8
            case $firstHex === 'cd': // uint 16
            case $firstHex === 'ce': // uint 32
            case $firstHex === 'cf': // uint 64
            case $firstHex === 'd0': // int 8
            case $firstHex === 'd1': // int 16
            case $firstHex === 'd2': // int 32
            case $firstHex === 'd3': // int 64
                return self::decodeInt($string);
            case in_array(hexdec($firstOctal), array(10, 11)): // fixstr 101xxxxx
            case $firstHex === 'd9': // str 8
            case $firstHex === 'da': // str 16
            case $firstHex === 'db': // str 32
                return self::decodeString($string);
            case $firstHex === 'c2': // false
                return array(false, substr($string, 1));
            case $firstHex === 'c3': // true
                return array(true, substr($string, 1));
            case $firstHex === 'c0': // nil
                return array(null, substr($string, 1));
            default:
                throw new UndecodeableException($firstHex . ' is not a valid identifier for a type.', 1);
        }
    }
653073
private function unwrap(string &$string): array
    {
        if (
            !$this->unaChecked
            &&
            \strpos($string, 'UNA') === 0
        ) {
            $this->analyseUNA(
                \substr(\substr($string, 3), 0, 9)
            );
        }

        if (
            !$this->unbChecked
            &&
            \strpos($string, 'UNB') === 0
        ) {
            $this->analyseUNB(
                \preg_replace(
                    "#^UNB\+#",
                    '',
                    \substr($string, 0, 8)
                )
            );
        }

        $string = \preg_replace(
            "/(([^" . $this->symbRel . "]" . $this->symbRel . "{2})+|[^" . $this->symbRel . "])" . $this->symbEnd . "/",
            "$1" . $this->stringSafe,
            $string
        );

        $file = \preg_split(
            self::$DELIMITER . $this->stringSafe . self::$DELIMITER . "i",
            $string
        );
        // fallback
        if ($file === false) {
            $file = [];
        }

        $end = \stripslashes($this->symbEnd);
        foreach ($file as $fc => &$line) {
            if (\trim($line) == '') {
                /* @noinspection OffsetOperationsInspection */
                unset($file[$fc]);
            }
            $line .= $end;
        }

        return $file;
    }
1243973
private function unserialize($string)
    {
        if (false === $string || null === $string || '' === $string) {
            throw new \InvalidArgumentException('Unable to unserialize value.');
        }
        set_error_handler(
            function () {
                restore_error_handler();
                throw new \InvalidArgumentException('Unable to unserialize value, string is corrupted.');
            },
            E_NOTICE
        );
        $result = unserialize($string);
        restore_error_handler();

        return $result;
    }
562901
public function paramsToString(array $params, $assoc = true)
    {
        $string = [];
        foreach ($params as $key => $param) {
            if (is_array($param)) {
                $string[] = $assoc
                    ? $key . '=' . $this->paramsToString($param, isAssocArray($param))
                    : $this->paramsToString($param, isAssocArray($param));
            } else {
                $string[] = $assoc ? $key . '=' . $param : $param;
            }
        }
        return join('&', $string);
    }
24425
public static function normalize($headers, $associative = true)
    {
        $normalizedHeaders = [];

        if (!$associative) {
            $headers = self::normalize($headers);
        }

        foreach (HeadersParser::parse($headers) as $name => $value) {
            if (strpos($value, 'HTTP/') === 0) {
                continue;
            }

            list($name, $value) = explode(':', $value, 2);

            $name = self::normalizeHeaderName($name);
            $value = self::normalizeHeaderValue($value);

            if (!$associative) {
                $normalizedHeaders[] = $name.': '.$value;
            } else {
                $normalizedHeaders[$name] = isset($normalizedHeaders[$name])
                    ? $normalizedHeaders[$name].', '.$value
                    : $value;
            }
        }

        return $normalizedHeaders;
    }
656128
public static function getArray(array $array, $associative = true)
    {
        $arrayText = "[";
        $index     = 0;
        $count     = count($array);
        foreach ($array as $key => $value) {
            if ($associative) {
                $arrayText .= static::getValue($key);
                $arrayText .= " => ";
            }
            $arrayText .= static::getValue($value);
            if ($index < $count - 1) {
                $arrayText .= ", ";
                $index++;
            }
        }
        return $arrayText . "]";
    }
766999
private static function fromHStore($string)
    {
        $result = [];
        if (empty($string)) {
            return $result;
        }

        preg_match_all(
            '#"((?:\\\"|[^"])*)"=>"((?:\\\"|[^"])*)"#U',
            $string,
            $matches
        );
        foreach ($matches[1] as $id => $val) {
            $result[stripcslashes($val)] = stripcslashes($matches[2][$id]);
        }

        return $result;
    }
295430
public function fetchAssoc($result = null)
    {
        $result = $result ? $result : $this->getResult();

        $rows = [];

        if ($result->rowCount() > 0) {
            $results = $result->fetchAll(\PDO::FETCH_ASSOC);
            foreach ($results as $key => $value) {
                $rows[$key] = $value;
            }
        }

        return $rows;
    }
440055
public function decodeArray($string, $fieldName = 'Unknown', $assoc = false)
    {
        if (empty($string)) {
            return [];
        }
        $array = self::decode(!empty($string) ? $string : '[]', $fieldName, $assoc);
        if (!$array || !is_array($array)) {
            throw new \Exception('The field '.$fieldName.' is not a JSON array as expected.');
        }

        return $array;
    }
66319
public static function q_assoc_id($q): array
    {
        $res = [];
        $qh = is_string($q) ? self::getInstance()->sql_query($q) : $q;

        while ($q = $qh->fetch(PDO::FETCH_ASSOC)) {
            $res[\current($q)] = $q;
        }

        return $res;
    }
1199564
public function decodeJson($assoc = false){

        foreach(self::$jsonable as $key){

            if(isset($this->{$key}) && Utils::isJson($$this->{$key})){
                $this->{$key} = json_decode($this->{$key}, $assoc);
            }
        }

        return $this;
    }
516694
public static function decode(string $json, bool $assoc = true)
    {
        $decoded = json_decode($json, $assoc);

        if ((unset) $decoded === $decoded) {
            throw new \InvalidArgumentException(sprintf(
                "Json decode failed (%s) with '%s' and data '%s'",
                json_last_error(),
                json_last_error_msg(),
                strlen($json) > 50 ? substr($json, 0, 150) . '...' : $json
            ));
        }

        return $decoded;
    }
645266
private static function decodeMap($string, $assoc)
    {
        $size = 0;
        $offset = 1;
        list($firstHex, $firstOctal) = self::analyzeFirstChar($string[0]);

        if ($firstOctal === '8') {
            // fixmap 1000xxxx
            $size = hexdec($firstHex) - bindec('10000000');
            $offset = 1;
        } elseif ($firstHex === 'de') {
            // map 16
            $size = hexdec(unpack('H*', substr($string, 1, 2)));
            $offset = 3;
        } elseif ($firstHex === 'df') {
            // map 32
            $size = hexdec(unpack('H*', substr($string, 1, 4)));
            $offset = 5;
        }

        // Strip off map data prefix.
        $string = substr($string, $offset);

        // Differentiate between an associative array and an object.
        if ($assoc) {
            $data = array();
            $assign = function ($key, $value) use (&$data) {
                $data[$key] = $value;
            };
        } else {
            $data = new stdClass;
            $assign = function ($key, $value) use ($data) {
                $data->$key = $value;
            };
        }

        // Recursively decode everything in this hashmap.
        for ($i = 0; $i < $size; $i++) {
            list($key, $string) = self::decodeRecursive($string, $assoc);
            list($value, $string) = self::decodeRecursive($string, $assoc);

            $assign($key, $value);
        }

        // Return the result.
        return array($data, $string);
    }
1272412
public static function smartUnserialize($str, $assoc = true)
    {
        if ($str[0] === '{' || $str[0] === '[') {
            return Json::parse($str, $assoc);
        }

        return Serialization::parse($str);
    }
1186778
public static function smartExplode($string)
    {
        if (empty($string))
            return array();

        if (is_array($string))
            return $string;

        if (!is_array($string)) {
            if (strpos($string, ';') !== false)
                return explode(';', trim($string, ';'));

            if (strpos($string, ',') !== false)
                return explode(',', trim($string, ','));
        }

        return (array)$string;
    }
641562
private function parseGroupedNestedFields(StringObject $string)
    {
        $output = $this->str('');
        $currentPath = [
            'array'  => [],
            'string' => ''
        ];

        $parts = $string->explode('[');
        $lastPart = $parts->count() - 1;

        foreach ($parts as $index => $part) {

            $fields = explode(',', $part);

            $isLast = $index == $lastPart;

            if (!$isLast) {
                $newNestedKey = array_pop($fields) . '.';
            }

            foreach ($fields as $field) {

                $fullPath = '';

                if (substr($field, 0, 1) == '!') {
                    $fullPath = '!';
                    $field = ltrim($field, '!');
                }

                $closingBrackets = substr_count($field, ']');
                $field = rtrim($field, ']');

                $fullPath .= $currentPath['string'] . $field;

                $output->append($fullPath . ',');

                if ($closingBrackets > 0) {
                    $currentPath['array'] = array_slice($currentPath['array'], 0, count($currentPath['array']) - $closingBrackets);
                    $currentPath['string'] = implode('.', $currentPath['array']);
                }
            }

            if (!$isLast) {
                $currentPath['string'] .= $newNestedKey;
                $currentPath['array'][] = $newNestedKey;
            }
        }

        return $output->trimRight(',');
    }