This code will reduce array deeply.
<?php
function print_s($s) {
return is_null($s) ? "NULL" : (is_array($s) ?
"Array" : ($s ? "TRUE" :
"FALSE"));
}
function r_and_dp($a, $b) {
echo "phase1:" . print_s($a) . "," .
print_s($b) . "<br>n";
if(is_array($a)) {
$a = array_reduce($a, "r_and_dp");
}
if(is_array($b)) {
$b = array_reduce($b, "r_and_dp");
}
echo "phase2:" . print_s($a) . "," .
print_s($b) . "<br>n";
$a = is_null($a) ? TRUE : $a;
$b = is_null($b) ? TRUE : $b;
echo "phase3:" . print_s($a) . "," .
print_s($b) . "<br>n";
return $a && $b;
}
$bools = array(TRUE, array(FALSE, TRUE), TRUE);
echo print_s(array_reduce($bools, "r_and_dp")) .
"<br>n";
// result: FALSE
?>
When using boolean, you have to carefully set an
"initial" argument.
<?php
function r_or_dp($a, $b) {
if(is_array($a)) {
$a = array_reduce($a, "r_or_dp");
}
if(is_array($b)) {
$b = array_reduce($b, "r_or_dp");
}
return (is_null($a) ? FALSE : $a) || (is_null($b) ? FALSE :
$b);
}
?>
----
Server IP: 219.94.145.73
Probable Submitter: 61.198.210.249
----
Manual Page -- http://www.php.net/manual/en/function.array-reduce.php
Edit -- https://master
.php.net/note/edit/72751
Del: integrated -- h
ttps://master.php.net/note/delete/72751/integrated
Del: useless -- http
s://master.php.net/note/delete/72751/useless
Del: bad code -- htt
ps://master.php.net/note/delete/72751/bad+code
Del: spam -- https:/
/master.php.net/note/delete/72751/spam
Del: non-english --
https://master.php.net/note/delete/72751/non-english
Del: in docs -- http
s://master.php.net/note/delete/72751/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/72751
Reject -- https://mast
er.php.net/note/reject/72751
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|