Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将查询字符串中的数组解析为数组,而不是字面上的“数组”;数组";_Php_Arrays_Uri_Query String_Eval - Fatal编程技术网

Php 将查询字符串中的数组解析为数组,而不是字面上的“数组”;数组";

Php 将查询字符串中的数组解析为数组,而不是字面上的“数组”;数组";,php,arrays,uri,query-string,eval,Php,Arrays,Uri,Query String,Eval,我遇到了一个不寻常的场景,需要将查询字符串转换为数组 查询字符串显示为: ?sort%5B0%5D%5Bfield%5D=type&sort%5B0%5D%5Bdir%5D=desc 解码为: sort[0][field]=type&sort[0][dir]=desc 如何将其作为可用数组输入PHP?i、 e echo $sort[0][field] ; // Outputs "type" 我尝试过邪恶的评估,但没有成功 我需要更好地解释,我需要的是sort%5B0%5D

我遇到了一个不寻常的场景,需要将查询字符串转换为数组

查询字符串显示为:

?sort%5B0%5D%5Bfield%5D=type&sort%5B0%5D%5Bdir%5D=desc
解码为:

sort[0][field]=type&sort[0][dir]=desc
如何将其作为可用数组输入PHP?i、 e

echo $sort[0][field] ; // Outputs "type"
我尝试过邪恶的评估,但没有成功


我需要更好地解释,我需要的是sort%5B0%5D%5Bfield%5D=type&sort%5B0%5D%5Bdir%5D=desc的文本字符串进入我的脚本并作为变量存储,因为它将作为函数中的参数传递


我该怎么做呢?

PHP将为您将该格式转换为数组

header("content-type: text/plain");
print_r($_GET);
给出:

Array
(
    [sort] => Array
        (
            [0] => Array
                (
                    [field] => type
                    [dir] => desc
                )

        )

)
如果您的意思是将该字符串作为字符串而不是作为查询字符串输入到您的网页中,则使用函数将其转换

header("content-type: text/plain");
$string = "sort%5B0%5D%5Bfield%5D=type&sort%5B0%5D%5Bdir%5D=desc";
$array = Array();
parse_str($string, $array);
print_r($array);
…提供相同的输出。

使用parse_str()



您不希望对直接从查询字符串中提取的字符串运行eval。这将是一个巨大的安全漏洞。我如何让parse_str()忽略查询字符串中的数组?这与您要求的正好相反…如果要获取原始查询字符串,请使用
$\u SERVER['query_string']
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
vecho $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz


parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>