Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用foreach循环获取下一个元素key的key_Php_Arrays - Fatal编程技术网

Php 如何使用foreach循环获取下一个元素key的key

Php 如何使用foreach循环获取下一个元素key的key,php,arrays,Php,Arrays,我从api获取xml数组,请在下面查看 我正在使用这个foreach循环 foreach ($event->raidstatus as $status){ //i want to get name and cound i tried $status->name but failed //and when i did print_r($stat

我从api获取xml数组,请在下面查看 我正在使用这个foreach循环

                    foreach ($event->raidstatus as $status){
                          //i want to get name and cound i tried $status->name but failed
                          //and when i did print_r($status) then got array
                          echo '<pre>'; print_r($status); exit;
                    }

请帮助我获取结果,谢谢

如果您只想从
元素中获取数据,那么您的
foreach()
循环只差1级

foreach ($event->raidstatus->children() as $status){
    echo '<pre>'.$status->name.PHP_EOL;
}
在获取整个
元素时,您希望所有子节点(在此代码中使用
children()
)获取名称元素

foreach($event->raidstatus->children()作为$status){
echo'.$status->name.PHP\u EOL;
SimpleXMLElement Object
(
    [status0] => SimpleXMLElement Object
        (
            [id] => 0
            [name] => Bestätigt
            [count] => 1
        )

    [status1] => SimpleXMLElement Object
        (
            [id] => 1
            [name] => Angemeldet
            [count] => 3
        )

    [status2] => SimpleXMLElement Object
        (
            [id] => 2
            [name] => Abgemeldet
            [count] => 4
        )

    [status3] => SimpleXMLElement Object
        (
            [id] => 3
            [name] => Ersatzbank
            [count] => 0
        )

    [required] => 40
)
}
您能否将XML显示为元素名为
status0
status1
等。是的,请看这个答案会帮助您,或者我不明白,您能否帮我解决这个问题,并给出与我的循环相关的答案?
foreach ($event->raidstatus->children() as $status){
    echo '<pre>'.$status->name.PHP_EOL;
}