-1

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/xxxx/xxx/xxxxxx.php 中为 foreach() 提供的参数无效 array(1) { ["RedeemTokenResult"]=> array(1) { ["DictionaryEntry" ]=> 空 } }

4

2 回答 2

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

keyValue($array['RedeemTokenResult']['DictionaryEntry']);
于 2012-08-22T20:38:35.610 回答
0

您应该使用数组而不是尝试创建一堆变量。您可以像这样创建一个动态的多维数组:

$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'];
于 2012-08-22T20:49:32.803 回答