0

我有一个响应,我正在构建一个数组。我不得不从原始响应中获取某些 id 来创建另一个休息连接器。代码看起来像这样(请原谅变量名,一段时间后我的想法用完了):

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 张发票中获取用户名,但用户名应该完全不同。当我 print_r($first} (这是响应中用户的名字)时,我得到了近 2,000 个数组条目,其中确实显示了这些不同的用户名。我已经搞砸了几个小时,但无法得到它。请帮助!

4

1 回答 1

0

尝试这个:

$xmlNew2 = new SimpleXMLElement($responseNew2);
$new_array = json_decode(json_encode($xmlNew2));

然后循环遍历$new_array。这应该更容易使用。

于 2012-08-24T04:07:02.883 回答