<code>$hasVideo = $details['data']['topic']['has_video'];</code>
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>
Is there an easier way? Or write a public method yourself to determine whether the value can be obtained at once.
<code>$hasVideo = $details['data']['topic']['has_video'];</code>
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>
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>
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>
Use count or current to judge, and multi-dimensional detection will be recursive.