如何访问序列化多维数组的值

时间:2012-08-27 16:26:33

标签: php arrays multidimensional-array

嗨,我有这个功能:

public static function consumeMessage($xml, $debug = 1) {
    $xml = stripslashes(urldecode($xml));
    $rq = OpenPayU::parseOpenPayUDocument($xml);    

    $msg = $rq['OpenPayU']['OrderDomainRequest'];

    switch (key($msg)) {
        case 'OrderNotifyRequest':
            return OpenPayU_Order::consumeNotification($xml);
            break;
        case 'ShippingCostRetrieveRequest':
            return OpenPayU_Order::consumeShippingCostRetrieveRequest($xml);
            break;
        default:
            return key($smg);
            break;
    }
}

我这样称呼:

$result = OpenPayU_Order::retrieve($result->sessionId);

以这种方式写入文件:

write_to_file("debug.txt", "order details: \n\n " .
serialize($result->response) . "\n\n");

在输出中我得到了:

  

一个:1:{S:8: “OpenPayU”;一个:2:{S:14: “HeaderResponse”;一个:3:{S:10: “发送者姓名”; S:8: “结帐”; S:7: “版本”; S:3: “1.0”; S:9: “算法”; S:3: “MD5”;} S:19: “OrderDomainResponse”;一个:1:{S:21: “OrderRetrieveResponse”;一个:8:{S:5: “残油”; S:32: “28444ffd9de5489e08a32e6cd51274a5”; S:6: “状态”;一个:1:{S:10: “的StatusCode”; S:16: “OPENPAYU_SUCCESS”;} S:13: “MerchantPosId”; S:5: “37857”; S:9: “的sessionid”; S:32: “55a6d45cc0fef40ce95e9c1321b19b13”; S:14: “的CustomerRecord”;一个:6:{ S:10: “客户ID”; S:8: “nyi3iw6v”; S:5: “电子邮件”; S:28: “netprofile@netprofile.com.pl”; S:5: “电话”; S:15: “(+48)   786265656 “; S:9:” 姓 “; S:5:” mieta “; S:8:” 姓 “; S:5:” mieta “; S:8:” 语言 “; S:2:” PL” ;} S:11: “OrderStatus”; S:20: “ORDER_STATUS_PENDING”; S:12: “货币代码”; S:3: “PLN”; S:7: “PayType”; S:1: “T”; }}}}

但是我需要获得几乎所有的值,f.ex:'StatusCode'并将其保存到数据库,有没有办法在没有正则表达式的情况下获取它?

我试着把它变成$ result-> response ['statuscode'];但我什么都没得到

1 个答案:

答案 0 :(得分:0)

如果要从文件中反序列化字符串,请执行

$array = unserialize(file_get_contents('/path/to/file'));

这将为您提供$result->response中的数组,您可以像任何其他多维数组一样访问该数组,例如

$responseData = $result->response;
echo $responseData["OpenPayU"]["HeaderResponse"]["SenderName"];
…

如果您不知道如何访问多维数组,please refer to the PHP Manual