我是否可以从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){[&#34; RedeemTokenResult&#34;] =&gt; array(1){[&#34; DictionaryEntry&#34;] =&gt; NULL}}
答案 0 :(得分:1)
function keyvalue(&$array) {
$result = Array();
foreach($array as $row) {
$result[$row['Key']] = $row['Value'];
}
$array = $result;
}
keyValue($array['RedeemTokenResult']['DictionaryEntry']);
答案 1 :(得分: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']);
}
这会产生一个looks like:
的$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'];