Foreach - 确定键是否存在于每次迭代中

Foreach - determine if key exists in each iteration

我有以下数组。

我想弄清楚如何在 foreach 中创建一个 if 语句,该语句将根据任何单个索引是否具有 [Customer_Facing_Comments__r]

来设置 true 或 false

我正在使用此代码 - 但是,这只是告诉我 [Customer_Facing_Comments__r] 是否在数组中 - 每次都是这样。我需要查看它是否在每个索引 [0]、[1]..等中...并以此为基础设置 true 或 false。

代码:

    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
                )

只需在 foreach 中公开密钥并在结果数组中使用它 $test:

foreach($queryResult->records as $key => $record){
    if (isset($record->Customer_Facing_Comments__r)) {
        $test[$key] = 'true';
    } else {
        $test[$key] = 'false';
    }
}