Php 递归查询字符串解析器

Php 递归查询字符串解析器,php,parsing,query-string,Php,Parsing,Query String,我需要一个将查询字符串解析为多维数组的函数。 查询字符串可能看起来像这样: ?fields=incidents:(id,shortdescription,action,request);people:(unid,name)&format=json array( 'fields' => array( 'incidents' => array( 'id', 'shortdescription', 'acti

我需要一个将查询字符串解析为多维数组的函数。 查询字符串可能看起来像这样:

?fields=incidents:(id,shortdescription,action,request);people:(unid,name)&format=json
array(
   'fields' => array(
      'incidents' => array(
         'id',
         'shortdescription',
         'action',
         'request'
      ),
      'people' => array(
         'unid',
         'name'
      ),
   ),
   'format' => 'json'
);
$separators = array(';', ':', ',');
返回的数组如下所示:

?fields=incidents:(id,shortdescription,action,request);people:(unid,name)&format=json
array(
   'fields' => array(
      'incidents' => array(
         'id',
         'shortdescription',
         'action',
         'request'
      ),
      'people' => array(
         'unid',
         'name'
      ),
   ),
   'format' => 'json'
);
$separators = array(';', ':', ',');
我希望分隔符是动态的,因此它们将驻留在如下数组中:

?fields=incidents:(id,shortdescription,action,request);people:(unid,name)&format=json
array(
   'fields' => array(
      'incidents' => array(
         'id',
         'shortdescription',
         'action',
         'request'
      ),
      'people' => array(
         'unid',
         'name'
      ),
   ),
   'format' => 'json'
);
$separators = array(';', ':', ',');
此数组中分隔符的顺序将决定查询字符串的解析顺序

有人手边有这样的东西吗

问候,

标记以开始:

$final = array();
$keys = explode("&", $_SERVER['QUERY_STRING']);
foreach ($keys as $key => $value) {

    $final[$key] = $value;

    $child = explode(";", $value);
    foreach ($child as $key2 => $value2) {
        $final[$key][$key2] = array();

        $subchild = explode(":", $value2);
        foreach ($subchild as $key3 => $value3) {
            $subsubchild = explode(",", $value3);
            $final[$key][$key2][$key3] = $subsubchild;
        }
    }
}
我没有测试这个,但希望能知道我要去哪里


可以对其进行更新,以创建一个接受分隔符的函数,使其真正具有递归性,这样您就可以调用此函数,而不是在循环中的循环中调用此函数…

我自己提出了以下建议:

protected static function split( $value, $sep = ',' )
{   
    if ( str_contains( $value, $sep ) ) 
        $value = explode( $sep, $value );

    return $value;
} 

protected static function multi_split( $value )
    {
        $arr = array();

        // Do we have multiple models?
        $parts = static::split( $value, '+' );        
        if ( count( $parts ) > 1 ) {
            foreach( $parts as $part ) {
                $models = static::split( $part, ':' ); 
                if ( count( $models ) > 1 ) {
                    $fields = static::split( trim($models[1], '()'), ',' );                   
                    $arr[$models[0]] = $fields;   
                }   
            }             

            return $arr;  
        } 

        // Do we have a single model?
        $parts = static::split( $value, ':' ); 
        if ( count( $parts ) > 1 ) {
            $fields = static::split( trim($parts[1], '()'), ',' );               
            $arr[$parts[0]] = $fields;

            return $arr;  
        } 

        // Do we have a comma-separated param?
        $parts = static::split( $value, ',' );
        if ( count( $parts ) > 1 ) 
            return $parts; 

        // Do we have a string param?
        return $value;

    }

有很多重复,我想摆脱。而且,它还不是很有活力

……到目前为止你都试了些什么?