解析 Web 服务 Soap Return - PHP 数组

Parse a Webservice Soap Return - PHP Array

我正在使用 returns var_export/var_dump(不太确定)的 PHP 数组的 Webservice Soap。我需要将响应转换为实际的 php 数组或获取 [VALUE] 中特定数组的值作为列表或 xml:公式、Aprueba、Describe 等

提前致谢!

      <getAttribDataResponse xmlns="urn:admin">
         <return>Array
(
    [0] => Array
        (
            [CDATTRIBUTE] => 49
            [NMATTRIBUTE] => SD1-ACCION
            [FGDATATYPE] => 1
            [FGATTRIBUTETYPE] => 1
            [NMLABEL] => Doc_Acción de la Normativa
            [FGMULTIVALUED] => 1
            [VALUE] => Array
                (
                    [1721] => Formula
                    [1477] => Aprueba
                    [1486] => Describe
                    [1506] => Reglamenta
                    [1522] => Constituye
                    [2128] => Agrega
                    [1485] => Deroga
                    [1497] => Oficializa                  
                )

        )

)</return>
      </getAttribDataResponse>

好的,我们开始,正则表达式路由。不过总的来说,如果有一个包可以解析我正在处理的数据,我会尝试使用它。

// Copied straight off from <return> in the response,
// shortened for readability
$str = 'Array
(
    [0] => Array
    ...

)';

// 1st regexp to extract the content lines of [VALUE]
preg_match(
    '/\[VALUE\] => Array[^\(]*\(\s*\n([^\)]+)\n\s+\)\n/',
    $str,
    $matches
);

// $matches[0] is what matched the whole regexp,
// $matches[1].. is for any parentheses inside it.
// We want the contents of the 1st parenthesis, so $matches[1]
$value_contents = $matches[1];

// 2nd regexp to extract the key/value pairs of [VALUE]
// One parenthesis for the key and one for the value,
// so $macthes[1] and $matches[2]
preg_match_all(
    '/\[([^\]]+)\] => (.+)/',
    $value_contents,
    $matches
); 

在这种情况下我们得到:

print_r($matches);
//    Array
//    (
//        [0] => Array
//            (
//                [0] => [1721] => Formula
//                [1] => [1477] => Aprueba
//                ...
//            )

//        [1] => Array
//            (
//                [0] => 1721
//                [1] => 1477
//                ...
//            )

//        [2] => Array
//            (
//                [0] => Formula
//                [1] => Aprueba
//                ...
//            )
//    )