To find a recursive function

Source: Internet
Author: User
The younger brother wants a recursive function, can judge whether the multidimensional array is. such as Array (array (), array (array (), array ()), the decision is null, but as long as there is an array containing non-null elements, the decision is not NULL, for example: Array (), Array (Array (' a '), Array ( )));

????????????????????????????????
I use CI to write a function, but there is no way to accurately determine;
function Is_empty ($mixed) {
if (Is_array ($mixed) &&!empty ($mixed)) {
foreach ($mixed as $v) {
$this->is_empty ($v);
}
}
if ($mixed = = = ' 0 ' | | $mixed = = 0 | | $mixed = = = ' False ' | | $mixed = = = False | | $mixed = = = ' null ') {
return-1;
}
ElseIf (!empty ($mixed)) {
return-1;
}
else{
return 1;
}
}
?????? -????????????????????????????????????
Ask God for help


Reply to discussion (solution)

function Is_empty ($mixed) {
if (Is_array ($mixed) &&!empty ($mixed)) {
foreach ($mixed as $v) {
if ($this->is_empty ($v) = =??) return 1;
}
.....

The red must be!
But I'm confused by you.
By
if ($mixed = = = ' 0 ' | | $mixed = = 0 | | $mixed = = = ' False ' | | $mixed = = = False | | $mixed = = = ' null ') {
return-1;
}
1 when NULL is returned
But
ElseIf (!empty ($mixed)) {
return-1;
}
It's not empty again. Return-1

if ($mixed = = = ' 0 ' | | $mixed = = 0 | | $mixed = = = ' False ' | | $mixed = = = False | | $mixed = = = ' null ') {
return-1;
}
The meaning of this paragraph is not to exclude the value of the variable may be the string ' 0 ', ' false ', ' null ' or integer 0, so it is all equal, the other non-null entries return-1, NULL returns 1;

As you say, the loop adds an if statement, but it still doesn't work.
????????????????????????????????????
if (Is_array ($mixed) &&!empty ($mixed)) {
foreach ($mixed as $v) {
if ($this->is_empty ($v) = =-1) {
return-1;
}
}
}
if ($mixed = = = ' 0 ' | | $mixed = = 0 | | $mixed = = = ' False ' | | $mixed = = = ' null ') {
return-1;
}
ElseIf (!empty ($mixed)) {
return-1;
}
else{
return 1;
}

That's the change.
if ($this->is_empty ($v) = =??) return 1;
For
if ($this->is_empty ($v) = = 1) return 1;

It might be better to write.

$a = Array (array (), array (1), array ()), $b = Array (), array (array (), array ()), Echo no_empty ($a), Echo No_ Empty ($b);//check whether the null function No_empty ($ar) {  if (! Is_array ($ar)) return 1;  $res = 0;  foreach ($ar as $v) {    if (Is_array ($v)) $res |= no_empty ($v);    else $res = 1;  }  return $res;}

Success ~ The original function of the Order of judgment changed a bit to succeed
-------------------------------------------------------------------------------------
function No_empty ($ar) {
Exclude string ' 0 ', ' false ', ' null ' and int 0, Boolean false;
if ($ar = = = ' 0 ' | | $ar = = 0 | | $ar = = = ' False ' | | $ar = = = False | | $ar = = = ' null ') {
return 1;
}
If it is not an array and is not the above value, directly determines whether it is empty
ElseIf (! Is_array ($ar)) {
if (!empty ($ar)) {
return 1;
}
return-1;
}
Recursive
else{
Initialize $res
$res =-1;
foreach ($ar as $v) {
As long as there is a non-empty element value in the multidimensional array, it is recorded.
if ($this->no_empty ($v) = = = 1) {
$res = 1;
}
}
return $res;
}
}

Can I give myself a minute?

Learn how to play.

  • 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.