Brian Powell
Brian Powell

Reputation: 3411

accessing specific elements of arrays in a foreach loop

I apologize if this is a very simple question - I've read through tons of posts here, but my question is syntactically very hard to search for, so I haven't found an answer yet.

I have a json array that's output from a company's 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] => 
                        )

                )

        )

)

and I'm trying to access the [ComID] and [Comment] elements, if they exist, inside of a foreach loop and assign it to the variable $y

So far I have:

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

but this gives me zero results. I understand WHY, because in between ['cData'] and ['dto'] are [0], [1], [2] etc.. elements, and I don't know how to add a qualifier for those into the loop.

Update

This code works for most of the json response:

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

However - I'm having one more small issue. If there are multiple [dto] responses, you'll have [dto][0][ComID] then [dto][1][ComID] and [dto][2][ComID], (like in the example above,) but if there's only ONE response, you'll have [dto][ComID] as there's no need for that middle array.

I tried writing if(array_key_exists('dto[0]' to execute one, then an else statement in the event dto[0] doesn't exist, but that didn't work. I need a way of NOT executing a foreach loop if there is no array underneath it to "foreachicize". Is there an if/else statement I can write to accommodate this?

Upvotes: 1

Views: 84

Answers (2)

AbraCadaver
AbraCadaver

Reputation: 78994

Probably need a nested foreach:

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

For the update to the question. Check if $i['dto'][0] exists:

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']}";
    }
}

There might be a better way but I'm headed out.

Upvotes: 1

henrik
henrik

Reputation: 1618

Another approach:

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']}";
        }
    }
}

Upvotes: 0

Related Questions