从XML源的关联数组创建PHP变量


Creating PHP Variables from Associative Array from XML Source

a我如何从Web服务的XML输出中获取下面的数组,提取[Key]和它们的[Value],并将它们转换为PHP变量(键与其值匹配),以便在PHP脚本中使用它们。运气不好,做了一些研究,我在任何地方都没有看到这种布局。这个布局看起来不一样,我找不到任何特定布局转换的例子。有没有办法从下面提取特定的字符串数据,比如"user.id",并将其值"431349"分配为php变量?

Array
(
    [RedeemTokenResult] => Array
        (
            [DictionaryEntry] => Array
                (
                    [0] => Array
                        (
                            [Key] => environmentname
                            [Value] => Staging
                        )
                    [1] => Array
                        (
                            [Key] => user.prc.type
                            [Value] => C
                        )
                    [2] => Array
                        (
                            [Key] => user.country
                            [Value] => United States
                        )
                    [3] => Array
                        (
                            [Key] => user.displayname
                            [Value] => Corporate User
                        )
                    [4] => Array
                        (
                            [Key] => sso.testmode
                            [Value] => True
                        )
                    [5] => Array
                        (
                            [Key] => user.region
                            [Value] => Texas
                        )
                    [6] => Array
                        (
                            [Key] => user.par.seg.id
                            [Value] => 1
                        )
                    [7] => Array
                        (
                            [Key] => user.postalcode
                            [Value] => 10001
                        )
                    [8] => Array
                        (
                            [Key] => session.expire.timeout
                            [Value] => 60
                        )
                    [9] => Array
                        (
                            [Key] => user.lastname
                            [Value] => User
                        )
                    [10] => Array
                        (
                            [Key] => user.par.classoftrade
                            [Value] => ALL
                        )
                    [11] => Array
                        (
                            [Key] => user.id
                            [Value] => 431349
                        )
                    [12] => Array
                        (
                            [Key] => user.firstname
                            [Value] => Corporate
                        )
                    [13] => Array
                        (
                            [Key] => user.par.id
                            [Value] => 0
                        )
                    [14] => Array
                        (
                            [Key] => user.city
                            [Value] => test
                        )
                    [15] => Array
                        (
                            [Key] => user.address1
                            [Value] => test
                        )
                    [16] => Array
                        (
                            [Key] => user.profile.companyname
                            [Value] => TradeOne
                        )
                    [17] => Array
                        (
                            [Key] => user.email
                            [Value] => nickk@tradeonemktg.com
                        )
                    [18] => Array
                        (
                            [Key] => user.username
                            [Value] => tomsnac
                        )
                    [19] => Array
                        (
                            [Key] => sso.client.hostname
                            [Value] => 10.1.2.135
                        )
                    [20] => Array
                        (
                            [Key] => session.expire.url
                            [Value] => https://ssostage.tradeonemktg.com/ReturnUrl=https%3a%2f%2fprcstage3.tradeonemktg.com%2fv%2f6%2f1%2fprc%2fsn%2fLogin.aspx%3freturnurl%3d%252fv%252f6%252f1%252fprc%252fsn%252fHome.aspx
                       )
                    [21] => Array
                        (
                            [Key] => sso.client.ip4
                            [Value] => 10.1.2.135
                        )
                    [22] => Array
                        (
                            [Key] => user.timezone
                            [Value] => (GMT-06:00) Central Time (US & Canada)
                        )
                    [23] => Array
                        (
                            [Key] => user.culture
                            [Value] => en-US
                        )
                    [24] => Array
                        (
                            [Key] => user.par.seg.marketcode
                            [Value] => NDC
                        )
                    [25] => Array
                        (
                            [Key] => user.telephone
                            [Value] => 111111111
                        )
                    [26] => Array
                        (
                            [Key] => session.expire.time
                            [Value] => 7/23/2012 12:38:28 PM
                        )
                    [27] => Array
                        (
                            [Key] => sso.xfer.parameters
                            [Value] => 
                        )
                    [28] => Array
                        (
                            [Key] => sso.timestamp
                            [Value] => 7/23/2012 11:38:31 AM
                        )
                )
        )
)

========================这是我正在处理的PHP文件。。。。

<?
require_once "SSO.class.php";
$service = 'https://XXX.XXX.XXX.com/SSO/XXXXXX.asmx'; //Service URl from TradeOne
$affiliate = 'XXXXXXXXXXXXXXXXXXXXXXX'; // Affiliate from TradeOne
$pass = XXXXXXXXXXXXXXXXXXXXXXX; //Password secret from TradeOne
$token = $_REQUEST['token'];
$sso = new SSO($pass);
$hashedToken = $sso->encryptToken($token,$salt,$iv);
print $sso->toString();
require_once "../soap_lib/nusoap.php";
$client   = new nusoap_client($service.'?WSDL',true);
$tok = array(
             'affiliate'=>$affiliate,
             'hashedToken'=>$hashedToken
             );
$response = $client->call('RedeemToken',array('parameters'=>$tok)); 
echo '<br /><br />';
function keyValue(&$response) {
  $result = Array();
  foreach($response as $row) {
    $result[$row['Key']] = $row['Value'];
  }
  $a = $result;
}
keyValue($a['RedeemTokenResult']['DictionaryEntry']);

var_dump($response);
?>

这是我做的最后一次编辑,结果是:

警告:为第51行/xxxx/xxxx/xx/xxxx/xxxxxx.php中的foreach()提供的参数无效array(1){["RedeemTokenResult"]=>array(2){"DictionaryEntry"]=>NULL}}

function keyvalue(&$array) {
  $result = Array();
  foreach($array as $row) {
    $result[$row['Key']] = $row['Value'];
  }
  $array = $result;
}
keyValue($array['RedeemTokenResult']['DictionaryEntry']);

您应该使用数组,而不是试图创建一堆变量。您可以创建一个动态的多维数组,如下所示:

$result = array();
foreach( $response['RedeemTokenResult']['DictionaryEntry'] as $item) {
    $keys = explode( '.', $item['Key']);
    $array_name = array_shift( $keys); // Get name of the array
    $temp =& $result[$array_name];
    foreach( $keys as $key)
    {           
        $temp =& $temp[$key];
    }
    $temp = trim( $item['Value']);
}

这会生成一个单独的$result阵列,看起来像:

Array
(
    [user] => Array
        (
            [telephone] => 111111111
        )
    [session] => Array
        (
            [expire] => Array
                (
                    [time] => 7/23/2012 12:38:28 PM
                )
        )
    [sso] => Array
        (
            [xfer] => Array
                (
                    [parameters] => 
                )
        )
)

现在您可以获取以下元素:

echo $result['user']['telephone'];
echo $result['session']['expire']['time'];