How the PHP multidimensional array determines whether it is empty

Source: Internet
Author: User
$hasVideo = $details['data']['topic']['has_video'];

For example such a piece of code, because do not know before the array ' data ', ' topic ' when there is value, need to determine the value of the assignment operation, how to easily judge to avoid the assignment times wrong.
If you only use the empty method, it seems to require such a layer of judgment in:

if(!empty($details)&&!empty($details['data'])&&!empty($details['data']['topic'])&&!empty($details['data']['topic']['has_video']))

Is there a simpler way? or write a public method of your own, and you can judge whether you can get the value at once.

Reply content:

$hasVideo = $details['data']['topic']['has_video'];

For example such a piece of code, because do not know before the array ' data ', ' topic ' when there is value, need to determine the value of the assignment operation, how to easily judge to avoid the assignment times wrong.
If you only use the empty method, it seems to require such a layer of judgment in:

if(!empty($details)&&!empty($details['data'])&&!empty($details['data']['topic'])&&!empty($details['data']['topic']['has_video']))

Is there a simpler way? or write a public method of your own, and you can judge whether you can get the value at once.

issetIt's OK to judge it by yourself.

$hasVideo = isset($details['data']['topic']['has_video']) ? $details['data']['topic']['has_video'] : $something_else;

may be oneself commit two, also was cheated by colleague. In fact, isset and empty can be judged.
I made a small test and found that I could not make an error.


  
   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']));?>

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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.