Home > Backend Development > PHP Tutorial > How to determine whether a multidimensional array in php is empty

How to determine whether a multidimensional array in php is empty

WBOY
Release: 2016-09-21 14:12:54
Original
1021 people have browsed it

<code>$hasVideo = $details['data']['topic']['has_video'];</code>
Copy after login
Copy after login

For example, in this piece of code, because we don’t know when the previous arrays ‘data’ and ‘topic’ have values, we need to judge whether they have values ​​before performing the assignment operation. How can we make a simple judgment to avoid errors when assigning values?
If you only use the empty method, it seems that you need to make judgments layer by layer like this:

<code>if(!empty($details)&&!empty($details['data'])&&!empty($details['data']['topic'])&&!empty($details['data']['topic']['has_video']))</code>
Copy after login
Copy after login

Is there an easier way? Or write a public method yourself to determine whether the value can be obtained at once.

Reply content:

<code>$hasVideo = $details['data']['topic']['has_video'];</code>
Copy after login
Copy after login

For example, in this piece of code, because we don’t know when the previous arrays ‘data’ and ‘topic’ have values, we need to judge whether they have values ​​before performing the assignment operation. How can we make a simple judgment to avoid errors when assigning values?
If you only use the empty method, it seems that you need to make judgments layer by layer like this:

<code>if(!empty($details)&&!empty($details['data'])&&!empty($details['data']['topic'])&&!empty($details['data']['topic']['has_video']))</code>
Copy after login
Copy after login

Is there an easier way? Or write a public method yourself to determine whether the value can be obtained at once.

Just use isset to judge whether it is OK.

<code class="php">$hasVideo = isset($details['data']['topic']['has_video']) ? $details['data']['topic']['has_video'] : $something_else;</code>
Copy after login

Maybe I made a mistake, or I was deceived by my colleagues. . In fact, isset and empty can be judged. .
I did a small test and found that no error will be reported.

<code><?php
    $test = array(
        test1=>array(
            test2=>array(
                test3=>array(
                    test4=>123,
                ),
            ),
        ),
    );
    var_dump(isset($test['test1']['test2']['test3']['test4']['test4']['test4']['test4']));
    var_dump(!empty($test['test1']['test2']['test3']['test4']['test4']['test4']['test4']));
?></code>
Copy after login

Use count or current to judge, and multi-dimensional detection will be recursive.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template