将爆炸结果转换为键、值中的数组
convert explode results to array in key, value
我有
这样的数据
list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr
使用 explode
在 array
中转换它并得到类似
的结果
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")
那么我如何转换成数组
检查这个
<?php
$string = "list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr";
$info = explode("&",$string);
$result = array();
foreach ($info as $val) {
$sub = explode("=",$val);
$result[$sub[0]] = $sub[1];
}
echo "<pre>".print_r($result,1)."</pre>";
?>
结果:
Array
(
[list] => 7251270233
[total_amount] => 28841.28
[r_g_amount] => 1800
[cash] => 1000
[credit] => 500
[bank] => 300
[bank_from] => abc
[bank_to] => pqr
)
<?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
)
使用 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
)
我有
这样的数据list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr
使用 explode
在 array
中转换它并得到类似
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")
那么我如何转换成数组
检查这个
<?php
$string = "list=7251270233&total_amount=28841.28&r_g_amount=1800&cash=1000&credit=500&bank=300&bank_from=abc&bank_to=pqr";
$info = explode("&",$string);
$result = array();
foreach ($info as $val) {
$sub = explode("=",$val);
$result[$sub[0]] = $sub[1];
}
echo "<pre>".print_r($result,1)."</pre>";
?>
结果:
Array
(
[list] => 7251270233
[total_amount] => 28841.28
[r_g_amount] => 1800
[cash] => 1000
[credit] => 500
[bank] => 300
[bank_from] => abc
[bank_to] => pqr
)
<?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
)
使用 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
)