将查询字符串转换为数组

时间:2015-06-27 16:50:19

标签: php arrays

我需要转换以下查询字符串的帮助:

status=SUCCESS&key=9bfad767-abb7-4147-b407-5cec175daa9e&invoice=2712&customer_account=&transaction_id=&fee=&total=70.0&test=true&reference=&process_code=¤cy=&exchange_rate=&reference_number=&message=

Array
(
[status] => SUCCESS
[key] => 9bfad767-abb7-4147-b407-5cec175daa9e
[invoice] => 2712
[customer_account] => 
[transaction_id] => 
[fee] => 
[total] => 70.0
[test] => true
[reference] => 
[process_code] => 
[currency] => 
[exchange_rate] => 
[reference_number] => 
[message] => 
)

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作

$str = 'status=SUCCESS&key=9bfad767-abb7-4147-b407-5cec175daa9e&invoice=2712&customer_account=&transaction_id=&fee=&total=70.0&test=true&reference=&process_code=¤cy=&exchange_rate=&reference_number=&message=';

parse_str($str, $output);

print_r($output);
echo $output['status'];
echo $output['key'];
......

Read more

答案 1 :(得分:0)

用于循环和爆炸,代码如下所示


    <?php
    $string = "status=SUCCESS&key=9bfad767-abb7-4147-b407-5cec175daa9e&invoice=2712&customer_account=&transaction_id=&fee=&total=70.0&test=true&reference=&process_code=¤cy=&exchange_rate=&reference_number=&message=";
    $fullarray = Array();
    $mainarray = explode("&",$string);
    for($i=0;$i<count($mainarray);$i++)
    {
        $tiny = explode("=",$mainarray[$i]);
        $fullarray[$tiny[0]] = $tiny[1];
    }
    print_r($fullarray);

    ?>

enter image description here