1

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):

enter image description here

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.

4
  • Possible duplicate of Check If array is null or not in php Commented Apr 16, 2019 at 11:43
  • @devgianlu As I've stated. empty() isn't working. Commented Apr 16, 2019 at 11:44
  • If you look further down, some user suggested to use isset. But anyway I think the solution has already been provided down here. Commented Apr 16, 2019 at 11:46
  • Adding some () around the OR statement did the trick Commented Apr 16, 2019 at 11:49

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, this works perfectly! Thanks a ton. Can you tell me why it has to be put between ( )?
@ScottvanDuin added note for that.
Was going to but I forgot, sorry!
@ScottvanDuin no issue, glad to help you :):)
1

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

Comments

0

Should work like this

if(in_array($decoded['tickets'][$int]['status'],['active','waiting on customer']) && empty($decoded['tickets'][$int]['assignedTo']))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.