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



我有以下数组:

我正试图找出如何在foreach中创建一个if语句,该语句将根据是否任何单个索引具有[Customer_Facing_Comments__r]

设置为true或false。我正在使用这段代码-然而,这只是告诉我如果[Customer_Facing_Comments__r]是在数组中-它是,每次。我需要看看它是否在每个索引[0],[1]等等…并在此基础上设置真或假。

代码:

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

最新更新