我有一个正在构建到数组中的响应。我不得不从原始响应中提取某些id来创建另一个rest连接器。代码看起来是这样的(请原谅变量名,过了一段时间我就没什么想法了):
foreach ($newArray as $r=>$p){
foreach ($p as $t=>$hoop){
checksession();
$restNew2 = new RESTConnector();
$urlNew2 = "https://localhost:9630/api/users/".$p['user']."/";
$restNew2->createRequest($urlNew2,"GET", null, $_SESSION['cookies'][0]);
$restNew2->sendRequest();
$responseNew2 = $restNew2->getResponse();
$xmlNew2 = new SimpleXMLElement($responseNew2);
$newerArray = array();
foreach ($xmlNew2->children() as $newestChild){
for($g=0, $count2 = count($xmlNew2); $g < $count2; $g++) {
$userID = (string)$xmlNew2['id'];
$first[] = (string)$xmlNew2->name->first;
$last[] = (string)$xmlNew2->name->last;
$newerArray[$g]['first'] = $first[$g];
}
}
}
}
如果我打印数组,它看起来像:
Array
(
[0] => Array
(
[first] => LightSpeed
)
[1] => Array
(
[first] => LightSpeed
)
[2] => Array
(
[first] => LightSpeed
)
[3] => Array
(
[first] => LightSpeed
)
[4] => Array
(
[first] => LightSpeed
)
[5] => Array
(
[first] => LightSpeed
)
[6] => Array
(
[first] => LightSpeed
)
[7] => Array
(
[first] => LightSpeed
)
[8] => Array
(
[first] => LightSpeed
)
[9] => Array
(
[first] => LightSpeed
)
[10] => Array
(
[first] => LightSpeed
)
[11] => Array
(
[first] => LightSpeed
)
[12] => Array
(
[first] => LightSpeed
)
[13] => Array
(
[first] => LightSpeed
)
[14] => Array
(
[first] => LightSpeed
)
[15] => Array
(
[first] => LightSpeed
)
[16] => Array
(
[first] => LightSpeed
)
[17] => Array
(
[first] => LightSpeed
)
[18] => Array
(
[first] => LightSpeed
)
}
我试图从18张发票中获取用户名,但用户名应该完全不同。当我打印_r($first}(这是响应中用户的名字)时,我得到了近2000个数组条目,其中确实显示了这些不同的用户名。我弄了好几个小时都弄不明白了。请帮帮我!
试试这个:
$xmlNew2 = new SimpleXMLElement($responseNew2);
$new_array = json_decode(json_encode($xmlNew2));
然后循环通过$new_array
。这应该更容易处理。