0

我有一个像这样的非序列化数组

Array (
    [status] => 1 
    [msg] => Transaction Fetched Successfully 
    [transaction_details] => Array ( 
        [100002982] => Array ( 
            [mihpayid] => 4149968 
            [request_id] => 635788 
            [bank_ref_num] => NINETE.31845.28052012 
            [amt] => 3295.00 
            [disc] => 0.00 
            [mode] => CC 
            [retries] => 0 
            [status] => success 
            [unmappedstatus] => captured 
        ) 
    ) 
)

我想解析这个数组,以便我可以获得状态值,即成功

我该怎么做

4

3 回答 3

2
$success = $array['transaction_details']['100002982']['status'];
于 2012-05-31T06:09:25.673 回答
0
$arr['transaction_details']['100002982']['status']
于 2012-05-31T06:06:11.367 回答
0

PHP 文档有一个用户注释,它建议了一种解析 print_r 输出的方法(类似于上面)。可以在这里找到:http ://www.php.net/manual/en/function.print-r.php#93529

这是在上述链接中找到的源代码的副本:

<?php 
function print_r_reverse($in) { 
    $lines = explode("\n", trim($in)); 
    if (trim($lines[0]) != 'Array') { 
        // bottomed out to something that isn't an array 
        return $in; 
    } else { 
        // this is an array, lets parse it 
        if (preg_match("/(\s{5,})\(/", $lines[1], $match)) { 
            // this is a tested array/recursive call to this function 
            // take a set of spaces off the beginning 
            $spaces = $match[1]; 
            $spaces_length = strlen($spaces); 
            $lines_total = count($lines); 
            for ($i = 0; $i < $lines_total; $i++) { 
                if (substr($lines[$i], 0, $spaces_length) == $spaces) { 
                    $lines[$i] = substr($lines[$i], $spaces_length); 
                } 
            } 
        } 
        array_shift($lines); // Array 
        array_shift($lines); // ( 
        array_pop($lines); // ) 
        $in = implode("\n", $lines); 
        // make sure we only match stuff with 4 preceding spaces (stuff for this array and not a nested one)
        preg_match_all("/^\s{4}\[(.+?)\] \=\> /m", $in, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); 
        $pos = array(); 
        $previous_key = ''; 
        $in_length = strlen($in); 
        // store the following in $pos: 
        // array with key = key of the parsed array's item 
        // value = array(start position in $in, $end position in $in) 
        foreach ($matches as $match) { 
            $key = $match[1][0]; 
            $start = $match[0][1] + strlen($match[0][0]); 
            $pos[$key] = array($start, $in_length); 
            if ($previous_key != '') $pos[$previous_key][1] = $match[0][1] - 1; 
            $previous_key = $key; 
        } 
        $ret = array(); 
        foreach ($pos as $key => $where) { 
            // recursively see if the parsed out value is an array too 
            $ret[$key] = print_r_reverse(substr($in, $where[0], $where[1] - $where[0])); 
        } 
        return $ret; 
    } 
} 

?>

我没有测试过上面的功能。

于 2012-05-31T06:23:59.047 回答