Voting

: eight minus one?
(Example: nine)

The Note You're Voting On

yuki [dot] kodama [at] gmail [dot] com
18 years ago
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);
}
?>

<< Back to user notes page

To Top