0

I have the following Array.

I am trying to figure out how to create an if statement in the foreach that will set true or false based on if any of the individual indexes has [Customer_Facing_Comments__r]

I am using this code - however, this just tells me if [Customer_Facing_Comments__r] is in the array - which it is, every time. I need to see if it is in each index [0], [1]..etc...and set true or false base on that.

CODE:

    foreach($queryResult->records as $record){
    if (array_key_exists("Customer_Facing_Comments__r",$record)) {
    $test = 'true'; }
    else { $test = 'false'; } }

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 5003xx0255mhcAAA
                    [Account] => stdClass Object
                        (
                            [Id] => 0010cxx026IwsTAAS
                            [Name] => xxx
                        )
                    [AccountId] => 0010c0xxwsTAAS
                    [Customer_Facing_Comments__r] => stdClass Object
                        (
                            [done] => 1
                            [queryLocator] => 
                            [records] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [Id] => 
                                            [Comment__c] => test string
                                            [CreatedDate] => 2021-02-13T00:25:50.000Z
                                            [Trouble_Ticket__c] => 5003x0000255mhcAAA
                                        )
                                )
                            [size] => 1
                        )
                    [Type] => Service Down
                )
            [1] => stdClass Object
                (
                    [Id] => 5003x000024Y6TpAAK
                    [Account] => stdClass Object
                        (
                            [Id] => 0010c0cccc6IwsTAAS
                            [Name] => test
                        )
                    [AccountId] => 0010c00cccIwsTAAS
                )
user2680315
  • 101
  • 7

1 Answers1

1

Just expose the key in the foreach and use that in your result array $test:

foreach($queryResult->records as $key => $record){
    if (isset($record->Customer_Facing_Comments__r)) {
        $test[$key] = 'true';
    } else {
        $test[$key] = 'false';
    }
}
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81