php 将查询字符串解析为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5397726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:15:57  来源:igfitidea点击:

Parse query string into an array

phparraysstring

提问by laukok

How can I turn a stringbelow into an array?

如何将下面的字符串转换为数组

pg_id=2&parent_id=2&document&video 

This is the array I am looking for,

这是我正在寻找的数组,

array(
    'pg_id' => 2,
    'parent_id' => 2,
    'document' => ,
    'video' =>
)

回答by Anthony

You want the parse_strfunction, and you need to set the second parameter to have the data put in an array instead of into individual variables.

您需要该parse_str函数,并且需要设置第二个参数以将数据放入数组中而不是放入单个变量中。

$get_string = "pg_id=2&parent_id=2&document&video";

parse_str($get_string, $get_array);

print_r($get_array);

回答by yassine2020

Sometimes parse_str()alone is note accurate, it could display for example:

有时parse_str()单独的音符是准确的,它可以显示例如:

$url = "somepage?id=123&lang=gr&size=300";

parse_str() would return:

parse_str() 将返回:

Array ( 
    [somepage?id] => 123 
    [lang] => gr 
    [size] => 300 
)

It would be better to combine parse_str()with parse_url()like so:

这将是更好地结合起来parse_str()parse_url(),像这样:

$url = "somepage?id=123&lang=gr&size=300";
parse_str( parse_url( $url, PHP_URL_QUERY), $array );
print_r( $array );

回答by Rocket Hazmat

Using parse_str().

使用parse_str().

$str = 'pg_id=2&parent_id=2&document&video';
parse_str($str, $arr);
print_r($arr);

回答by Ionut Bajescu

Use http://us1.php.net/parse_str

使用http://us1.php.net/parse_str

Attention, it's usage is:

注意,它的用法是:

parse_str($str, &$array);

not

不是

$array = parse_str($str);

回答by Casper Wilkes

If you're having a problem converting a query string to an array because of encoded ampersands

如果由于编码的&符号而将查询字符串转换为数组时遇到问题

&

then be sure to use html_entity_decode

那么一定要使用 html_entity_decode

Example:

例子:

// Input string //
$input = 'pg_id=2&parent_id=2&document&video';

// Parse //
parse_str(html_entity_decode($input), $out);

// Output of $out //
array(
  'pg_id' => 2,
  'parent_id' => 2,
  'document' => ,
  'video' =>
)

回答by KingCrunch

There are several possible methods, but for you, there is already a builtin parse_strfunction

有几种可能的方法,但对你来说,已经有一个内置parse_str函数

$array = array();
parse_str($string, $array);
var_dump($array);

回答by hovado

This is one-liner for parsing query from current URL into array:

这是将查询从当前 URL 解析为数组的单行代码:

parse_str($_SERVER['QUERY_STRING'], $query);

回答by hovado

You can use the PHP string function parse_str()followed by foreachloop.

您可以使用 PHP 字符串函数parse_str()后跟foreach循环。

$str="pg_id=2&parent_id=2&document&video";
parse_str($str,$my_arr);
foreach($my_arr as $key=>$value){
  echo "$key => $value<br>";
}
print_r($my_arr);

回答by Max Muster

// ok, another method using c4 ..    

// we have $input as string
$input = "pg_id=2&parent_id=2&document&video";

// looking for $output as array
$output= array(); 

// We knead a bit c4, and stick it to $input and turn it to pieces ...
$pieces = explode("&",$input);

// we pick up all the pieces and make the output array
foreach($pieces as $k => $v){
    // the new key is what is before the "="
    $var[$k] = explode("=",$pieces[$k])[0];
    // and the new value is what is after the "="
    $output[$var[$k]] = explode("=",$pieces[$k])[1];
}


// print_r($output);
Array
(
    [pg_id] => 2
    [parent_id] => 2
    [document] => 
    [video] => 
)

回答by Dewa Putra

This is the PHP code to split query in mysql & mssql

这是在 mysql 和 mssql 中拆分查询的 PHP 代码

enter code here
function splitquery($strquery)
{
$arrquery=explode('select',$strquery);

$stry='';$strx='';

for($i=0;$i<count($arrquery);$i++)
{
if($i==1)
{
    echo 'select '.trim($arrquery[$i]);
}
elseif($i>1)
{

$strx=trim($arrquery[($i-1)]);

    if(trim(substr($strx,-1))!='(')
    {
        $stry=$stry.'

select '.trim($arrquery[$i]);
    }
    else
    {
        $stry=$stry.trim('select '.trim($arrquery[$i]));
    }

$strx='';
}
} 

return $stry;
}

Example:

例子:

Query before

查询之前

select xx from xx select xx,(select xx) from xx where y=' cc' select xx from xx left join ( select xx) where (select top 1 xxx from xxx) oder by xxx desc ";

select xx from xx select xx,(select xx) from xx where y='cc' select xx from xx left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc ";

Query after

查询后

select xx from xx

从 xx 中选择 xx

select xx,(select xx) from xx where y=' cc'

select xx,(select xx) from xx where y=' cc'

select xx from xx left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc

select xx from xx left join (select xx) where (select top 1 xxx from xxx) oder by xxx desc

Thank you, from Indonesia Sentrapedagang.com

谢谢,来自印度尼西亚Sentrapedagang.com