5
回答
求教.解析字符串.还望大家来帮忙.
【腾讯云】学生服务器套餐10元/月 >>>   

小弟现在有一条数据,字符串格式.

需要转换成map对象,字符中每一条有效数据是以"|"间隔.

请大家帮忙.在线等.

trans_state = 01,
 dc_mark = 01,
 amount = 10,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 755,
 amt_type = 01,
 trans_time = 170158,
 order_id = OD201521200000002,
 trans_type = 03,
 order_date = 20150312 

trans_state = 01,
 dc_mark = 01,
 amount = 1,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 765,
 amt_type = 01,
 trans_time = 160631,
 order_id = OD201521200000001,
 trans_type = 03,
 order_date = 20150312 

trans_state = 01,
 dc_mark = 01,
 amount = 4,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 766,
 amt_type = 01,
 trans_time = 143756,
 order_id = OD201521200000023,
 trans_type = 03,
 order_date = 20150312 

<无标签>
举报
共有5个答案 最后回答: 3年前
<?php
$str = <<<EOT
trans_state = 01,
 dc_mark = 01,
 amount = 10,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 755,
 amt_type = 01,
 trans_time = 170158,
 order_id = OD201521200000002,
 trans_type = 03,
 order_date = 20150312
|
trans_state = 01,
 dc_mark = 01,
 amount = 1,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 765,
 amt_type = 01,
 trans_time = 160631,
 order_id = OD201521200000001,
 trans_type = 03,
 order_date = 20150312
|
trans_state = 01,
 dc_mark = 01,
 amount = 4,
 trans_date = 20150312,
 acc_check_date = 20150312,
 balance = 766,
 amt_type = 01,
 trans_time = 143756,
 order_id = OD201521200000023,
 trans_type = 03,
 order_date = 20150312
EOT;
//去除空白字符
$str = preg_replace('/\s+/', '', $str);
$arr = explode('|', $str);
foreach($arr as $k => $v) {
    $arr1 = explode(',', $v);
    foreach($arr1 as $v1) {
        $arr2 = explode('=', $v1);
        $m[$arr2[0]] = $arr2[1];

    }
    $map[$k] = $m;
}
print_r($map);
//输出:
Array
(
    [0] => Array
        (
            [trans_state] => 01
            [dc_mark] => 01
            [amount] => 10
            [trans_date] => 20150312
            [acc_check_date] => 20150312
            [balance] => 755
            [amt_type] => 01
            [trans_time] => 170158
            [order_id] => OD201521200000002
            [trans_type] => 03
            [order_date] => 20150312
        )

    [1] => Array
        (
            [trans_state] => 01
            [dc_mark] => 01
            [amount] => 1
            [trans_date] => 20150312
            [acc_check_date] => 20150312
            [balance] => 765
            [amt_type] => 01
            [trans_time] => 160631
            [order_id] => OD201521200000001
            [trans_type] => 03
            [order_date] => 20150312
        )

    [2] => Array
        (
            [trans_state] => 01
            [dc_mark] => 01
            [amount] => 4
            [trans_date] => 20150312
            [acc_check_date] => 20150312
            [balance] => 766
            [amt_type] => 01
            [trans_time] => 143756
            [order_id] => OD201521200000023
            [trans_type] => 03
            [order_date] => 20150312
        )

)
顶部