简体   繁体   English

检查数组中的数组在PHP中是否为空/空

[英]Checking if an array within an array is empty/null in PHP

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. 我试过检查是否为emptyinArrayinArray的常规条件。

Screenshot of the array ( assignedTo , which needs to be null/empty in order for the if to be true): 数组的屏幕截图( assignedTo ,如果为true,则必须为null / empty):

在此处输入图片说明

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. 因此,我希望在$decoded['tickets'][$int]['assignedTo']为空时执行if。 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 这将检查状态是active还是waiting on customer assignedTo不为空

应该这样工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM