访问foreach循环中数组的特定元素


accessing specific elements of arrays in a foreach loop

如果这是一个非常简单的问题,我很抱歉-我已经阅读了这里的大量帖子,但我的问题在语法上很难搜索,所以我还没有找到答案。

我有一个json数组,它是从一家公司的API输出的:

[Result] => Array
        (
            [cData] => Array
                (
                    [0] => Array
                        (
                            [Reqa] => ABCD
                            [Reqb] => 
                            [Reqc] => Plus
                            [dto] => Array
                                (
                                    [0] => Array
                                        (
                                            [ComID] => 43292392                                            
                                            [Comment] => Dave
                                        )
                                    [1] => Array
                                        (
                                            [ComID] => 43292392                                            
                                            [Comment] => Bob
                                        )
                                )
                            [XREFSearchOperation] => Exact
                        )
                    [1] => Array
                        (
                            [Reqa] => BCDE
                            [Reqb] => 
                            [Reqc] => A
                            [dto] => Array
                                (
                                    [0] => Array
                                        (
                                            [ComID] => 19331186                                            
                                            [Comment] => Mike
                                        )
                                    [1] => Array
                                        (
                                            [ComID] => 19331186
                                            [Comment] => Roger
                                        )
                                )
                            [XREFSearchOperation] => Starts With
                        )
                    [2] => Array
                        (
                            [Reqa] => QQDT
                            [Reqb] => 
                        )
                )
        )
)

我正试图访问foreach循环中的[ComID][Comment]元素(如果存在的话),并将其分配给变量$y

到目前为止,我有:

foreach ($json['Result']['cData']['dto'] as $i) {
 $y = "{$i['ComID']}|{$i['Comment']}";
}

但这给了我零结果。我理解WHY,因为在['cData']['dto']之间有[0][1][2] etc..元素,我不知道如何将这些的限定符添加到循环中。

更新

此代码适用于大部分json响应:

foreach ($json['Result']['cData'] as $i) {
    if(array_key_exists('dto', $i)) {
      foreach ($i['dto'] as $j) {
 $y = "{$j['ComID']}|{$j['Comment']}";
      } else {
        }      
    } 

然而,我还有一个小问题。如果有多个[dto]响应,则会有[dto][0][ComID][dto][1][ComID][dto][2][ComID](就像上面的例子一样),但如果只有一个响应,则有[dto][ComID],因为不需要中间的数组。

我尝试编写if(array_key_exists('dto[0]'来执行一个,然后事件dto[0]中的else语句不存在,但这不起作用。如果"foreachicize"下面没有数组,我需要一种不执行foreach循环的方法。有没有我可以写的if/else语句来适应这一点?

可能需要嵌套的foreach:

foreach ($json['Result']['cData'] as $i) {
    foreach($i['dto'] as $j) {
        $y[] = "{$j['ComID']}|{$j['Comment']}"; //need an array here
    }
}

关于问题的更新。检查$i['dto'][0]是否存在:

foreach ($json['Result']['cData'] as $i) {
    if(isset($i['dto'][0]))) {
        foreach($i['dto'] as $j) {
            $y[] = "{$j['ComID']}|{$j['Comment']}";
        }
    } else {
        $y[] = "{$j['ComID']}|{$j['Comment']}";
    }
}

也许还有更好的办法,但我要走了。

另一种方法:

foreach($json['Result']['cData'] as $cData) 
{
    foreach($cData['dto'] as $dto) 
    {
        if(array_key_exists('ComID', $dto) && array_key_exists('Comment', $dto))
        {
            $y = "{$dto['ComID']}|{$dto['Comment']}";
        }
    }
}