note 72751 added to function.array-reduce
| From: | yuki | Date: | Wed, 31 Jan 2007 16:56:17 +0000 |
| Subject: | note 72751 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-122429@lists.php.net to get a copy of this message | ||
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 -- https://master.php.net/note/delete/72751/integrated
Del: useless -- https://master.php.net/note/delete/72751/useless
Del: bad code -- https://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 -- https://master.php.net/note/delete/72751/in+docs
Del: other reasons-- https://master.php.net/note/delete/72751
Reject -- https://master.php.net/note/reject/72751
Search -- https://master.php.net/manage/user-notes.php