arrays - PHP Recursive Function, return error -
i create php function test if $var exist , not empty
.
but, have problem can't identify :
- in foreach, return false ignored because 'return' return first.
look :
function notempty($var, $r=false){ if(gettype($var)=='string' or gettype($var)=='integer'){ if($r==false){ if(isset($var) , @!empty($var)){ return true; }else{return false;} }else{ if(!isset($var) , @empty($var)){ return false; } } }elseif(gettype($var)=='array'){ foreach($var $val){ if(gettype($val)=='array'){ notempty($val, true); }else{ if(empty($val) or $val==''){echo "string"; return false; } } } return true; }else{return null;} }
i call function : - notempty(array($val1, $val2, $val3));
i guess more or less looking for:
<?php function notempty($array, $recursive=false) { $empty = array_filter($array, function($element) use ($recursive) { if (is_array($element) && $recursive) { return !notempty($element); } else { return empty($element); } }); return 0===count($empty); } var_dump(notempty([1, 2, 3])); var_dump(notempty(['foo', '', 'bar'])); var_dump(notempty(['foo', null, 'bar'])); var_dump(notempty(['foo', [1,2,3], 'bar'])); var_dump(notempty(['foo', [1,null,3], 'bar'])); var_dump(notempty(['foo', [1,null,3], 'bar'], true));
the output of is:
bool(true) bool(false) bool(false) bool(true) bool(true) bool(false)
Comments
Post a Comment