解析 API 结果 Returns 一个数组和字符串
Parse API Result That Returns an Array and String
我从 API 返回了一些数据,格式如下:
string(15) "hello@yahoo.com"
bool(true)
string(7) "generic"
array(5) {
["server"]=>
string(19) "imap.mail.yahoo.com"
["username"]=>
string(15) "hello@yahoo.com"
["port"]=>
int(993)
["use_ssl"]=>
bool(true)
["oauth"]=>
bool(false)
}
array(0) {
}
当我试图用 foreach
循环解析它时,它是不成功的。我假设这是由于位于实际数组上方的 string(15)
、bool(true)
和 string(7)
数据块造成的。
这是我的 foreach
循环:
$results = array();
foreach ($Request->getResults() as $imapSettings) {
$results['server'] = $imapSettings['server'];
$results['username'] = $imapSettings['username'];
$results['port'] = $imapSettings['port'];
$results['use_ssl'] = $imapSettings['use_ssl'];
$results['oauth'] = $imapSettings['oauth'];
}
当我运行上面的代码时,我得到以下错误:Warning: Illegal string offset 'server' in
。而且我相信这个错误通常表明某些东西不是数组。
所以我的问题是如何解析这段数据?
更新:
这是 var_dump($Request->getResults());
returns:
array(5) {
["email"]=>
string(15) "hello@yahoo.com"
["found"]=>
bool(true)
["type"]=>
string(7) "generic"
["imap"]=>
array(5) {
["server"]=>
string(19) "imap.mail.yahoo.com"
["username"]=>
string(15) "hello@yahoo.com"
["port"]=>
int(993)
["use_ssl"]=>
bool(true)
["oauth"]=>
bool(false)
}
["documentation"]=>
array(0) {
}
}
foreach 只适用于数组,所以你应该转义其他类型:
$results = array();
foreach ($Request->getResults() as $imapSettings) {
if (!is_array($imapSettings)) {
// you can do other stuff for other type
continue;
}
$results['server'] = $imapSettings['server'];
$results['username'] = $imapSettings['username'];
$results['port'] = $imapSettings['port'];
$results['use_ssl'] = $imapSettings['use_ssl'];
$results['oauth'] = $imapSettings['oauth'];
}
我想这就是你想要做的:
$response = $Request->getResults();
$results['server'] = $response['imap']['server'];
$results['username'] = $response['imap']['username'];
$results['port'] = $response['imap']['port'];
$results['use_ssl'] = $response['imap']['use_ssl'];
$results['oauth'] = $response['imap']['oauth'];
您无需遍历结果 - 您只需从结果中包含的 'imap' 数组中提取所需信息即可。
您也可以更简洁地执行此操作,这样会产生相同的结果:
$results = $response['imap'];
我从 API 返回了一些数据,格式如下:
string(15) "hello@yahoo.com"
bool(true)
string(7) "generic"
array(5) {
["server"]=>
string(19) "imap.mail.yahoo.com"
["username"]=>
string(15) "hello@yahoo.com"
["port"]=>
int(993)
["use_ssl"]=>
bool(true)
["oauth"]=>
bool(false)
}
array(0) {
}
当我试图用 foreach
循环解析它时,它是不成功的。我假设这是由于位于实际数组上方的 string(15)
、bool(true)
和 string(7)
数据块造成的。
这是我的 foreach
循环:
$results = array();
foreach ($Request->getResults() as $imapSettings) {
$results['server'] = $imapSettings['server'];
$results['username'] = $imapSettings['username'];
$results['port'] = $imapSettings['port'];
$results['use_ssl'] = $imapSettings['use_ssl'];
$results['oauth'] = $imapSettings['oauth'];
}
当我运行上面的代码时,我得到以下错误:Warning: Illegal string offset 'server' in
。而且我相信这个错误通常表明某些东西不是数组。
所以我的问题是如何解析这段数据?
更新:
这是 var_dump($Request->getResults());
returns:
array(5) {
["email"]=>
string(15) "hello@yahoo.com"
["found"]=>
bool(true)
["type"]=>
string(7) "generic"
["imap"]=>
array(5) {
["server"]=>
string(19) "imap.mail.yahoo.com"
["username"]=>
string(15) "hello@yahoo.com"
["port"]=>
int(993)
["use_ssl"]=>
bool(true)
["oauth"]=>
bool(false)
}
["documentation"]=>
array(0) {
}
}
foreach 只适用于数组,所以你应该转义其他类型:
$results = array();
foreach ($Request->getResults() as $imapSettings) {
if (!is_array($imapSettings)) {
// you can do other stuff for other type
continue;
}
$results['server'] = $imapSettings['server'];
$results['username'] = $imapSettings['username'];
$results['port'] = $imapSettings['port'];
$results['use_ssl'] = $imapSettings['use_ssl'];
$results['oauth'] = $imapSettings['oauth'];
}
我想这就是你想要做的:
$response = $Request->getResults();
$results['server'] = $response['imap']['server'];
$results['username'] = $response['imap']['username'];
$results['port'] = $response['imap']['port'];
$results['use_ssl'] = $response['imap']['use_ssl'];
$results['oauth'] = $response['imap']['oauth'];
您无需遍历结果 - 您只需从结果中包含的 'imap' 数组中提取所需信息即可。
您也可以更简洁地执行此操作,这样会产生相同的结果:
$results = $response['imap'];