将爆炸结果转换为键,值中的数组

时间:2017-05-06 05:17:51

标签: php arrays laravel explode

我有像

这样的数据

list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr

使用array将其转换为explode并获得

之类的结果
0:"list=7251270233"
1:"total_amount=28841.28"
2:"r_g_amount=1800"
3:"cash=1000"
4:"credit=500"
5:"bank=300"
6:"bank_from=abc"
7:"bank_to=pqr"

现在我将它转换为key->value中的数组,如:

array("list"=>"7251270233",
"total_amount"=>"28841.28",
"r_g_amount"=>"1800",
"cash"=>"1000",
"credit"=>"500",
"bank"=>"300",
"bank_from"=>"abc",
"bank_to"=>"pqr")

那我怎么能在数组中转换

2 个答案:

答案 0 :(得分:1)

使用parse_str

将其转换为数组非常简单

如下所示:

 $query_string = "list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr";

 $data_array = parse_str($query_string, $output);

print_r($output);

输出将如下:

Array
(
    [list] => 7251270233
    [total_amount] => 28841.28
    [r_g_amount] => 1800
    [cash] => 1000
    [credit] => 500
    [bank] => 300
    [bank_from] => abc
    [bank_to] => pqr
)

答案 1 :(得分:0)

<?php 
$str = 'list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr';
$strarr = explode('&',$str);
print_r($strarr);
foreach($strarr as $stra){
    $result[explode('=',$stra)[0]] = explode('=',$stra)[1];
}

print_r($result);

输出

Array
(
    [list] => 7251270233
    [total_amount] => 28841.28
    [r_g_amount] => 1800
    [cash] => 1000
    [credit] => 500
    [bank] => 300
    [bank_from] => abc
    [bank_to] => pqr
)

https://eval.in/788202