I'm checking an array if it contains a few things and an array inside it is empty. How do I achieve this?
I've tried checking it on empty
, inArray
and a regular condition in the if.
Screenshot of the array ( assignedTo
, which needs to be null/empty in order for the if to be true):
for($i = 1; $i < $this->pages['maxPages']+1; $i++) {
$response = $this->client->get('v1/tickets/search.json', [
'query' => ['page' => $i, 'assignedTo[]' => null, 'statuses[]' => 'active', 'waiting', 'sortDir' => 'desc', 'sortBy' => 'updatedAt']
]);
//Verwekt de data gekregen van de API
$json = (string)$response->getBody()->getContents();
$decoded = json_decode($json, true);
//Haalt de lengte van de result array op
$arrayLength = sizeof($decoded['tickets']);
//Slaat de resultaten op in de bijbehorende variabele. De if statement loopt nog na of de tickets écht aan de filters voldoen. Het komt nog wel eens voor dat er tickets worden opgehaald die niet opgehaald moeten worden volgens de filters.
for($int = 0; $int < $arrayLength; $int++) {
if($decoded['tickets'][$int]['status'] === 'active' || $decoded['tickets'][$int]['status'] === 'waiting on customer' && empty($decoded['tickets'][$int]['assignedTo'])) {
$newticketamount++;
$activetickets[] = $decoded['tickets'][$int];
}
}
}
So, I want the if to be executed when the $decoded['tickets'][$int]['assignedTo']
is empty. Currently it has both the tickets that ARE assigned and the ones that aren't.
Try to wrap ||
condition in one and then add &&
with it:-
if( empty( $decoded['tickets'][$int]['assignedTo'] ) && ($decoded['tickets'][$int]['status'] === 'active' || $decoded['tickets'][$int]['status'] === 'waiting on customer')){
$newticketamount++;
$activetickets[] = $decoded['tickets'][$int];
}
Even a much better approach [for readability as well as better implementation]
if( empty( $decoded['tickets'][$int]['assignedTo'] ) ){
if( ($decoded['tickets'][$int]['status'] === 'active' || $decoded['tickets'][$int]['status'] === 'waiting on customer')){
$newticketamount++;
$activetickets[] = $decoded['tickets'][$int];
}
}
Note:- because of Operator Precedence you faced problem.
Empty works perfectly fine, I think the problem is with logical operators and its order
You need to put parenthesis there
if (($decoded['tickets'][$int]['status'] === 'active' || $decoded['tickets'][$int]['status'] === 'waiting on customer') && empty($decoded['tickets'][$int]['assignedTo'])) {
This will check if status is active
or waiting on customer
AND assignedTo
is not empty
应该这样工作
if(in_array($decoded['tickets'][$int]['status'],['active','waiting on customer']) && empty($decoded['tickets'][$int]['assignedTo']))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.