递归计算数组布尔值的方法
Recursive method to calculate the Boolean value of an array
我正在开发一个 Laravel/php 应用程序,我有这个数组,我想在其中收集最终结果:
[
{
"nodeType": "or",
"0": {
"nodeType": "and",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
]
我希望能够收集到 True 或 False 的最终值。数组本身可以包含任意数量的 children,例如:
[
{
"nodeType": "or",
"0": {
"nodeType": "and",
"0": {
"nodeType": "or",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
]
解决这个问题的最佳方法是什么?我认为循环不起作用,因为数组的深度不固定。
编辑 1:
回答评论中的一些问题。钥匙不重要。节点总是有两个值。但是一个值可以有两个值,如第二个示例所示。
外层是一个数组,但这个数组只有一个条目。所以它可以简单地用作 array[0]
来到达 json object.
编辑 2:
以下数组的结果应该为假,但第一个答案 returns 为真。
{
"nodeType": "and",
"0": {
"nodeType": "and",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
nodetype
可以是 and
或 or
。它并不总是像上面示例中所写的那样。
您可以使用递归函数,如下所示:
function deduct($expr) {
if (is_bool($expr)) return $expr; // Base case
// If OR, we can stop when we find true (and return true).
// If AND, we can stop when we find false (and return false).
$search = $expr["nodeType"] == "or";
foreach ($expr as $key => $value) {
if ($key !== "nodeType" && deduct($value) === $search) return $search;
}
// If that never happened, return the opposite (false for OR, true for AND)
return !$search;
}
使用第二个 JSON 作为输入的调用示例:
$json = '[{"nodeType": "or","0": {"nodeType": "and", "0": {"nodeType": "or", "0": {"nodeType": "and","1": true,"2": false},"3": true},"3": true},"2": {"nodeType": "or","4": false,"5": true}}]';
$obj = json_decode($json, true)[0]; // The outer level is an array: unwrap it.
var_dump(deduct($obj));
输出:
bool(true)
我正在开发一个 Laravel/php 应用程序,我有这个数组,我想在其中收集最终结果:
[
{
"nodeType": "or",
"0": {
"nodeType": "and",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
]
我希望能够收集到 True 或 False 的最终值。数组本身可以包含任意数量的 children,例如:
[
{
"nodeType": "or",
"0": {
"nodeType": "and",
"0": {
"nodeType": "or",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
]
解决这个问题的最佳方法是什么?我认为循环不起作用,因为数组的深度不固定。
编辑 1:
回答评论中的一些问题。钥匙不重要。节点总是有两个值。但是一个值可以有两个值,如第二个示例所示。
外层是一个数组,但这个数组只有一个条目。所以它可以简单地用作 array[0]
来到达 json object.
编辑 2: 以下数组的结果应该为假,但第一个答案 returns 为真。
{
"nodeType": "and",
"0": {
"nodeType": "and",
"0": {
"nodeType": "and",
"1": true,
"2": false
},
"3": true
},
"2": {
"nodeType": "or",
"4": false,
"5": true
}
}
nodetype
可以是 and
或 or
。它并不总是像上面示例中所写的那样。
您可以使用递归函数,如下所示:
function deduct($expr) {
if (is_bool($expr)) return $expr; // Base case
// If OR, we can stop when we find true (and return true).
// If AND, we can stop when we find false (and return false).
$search = $expr["nodeType"] == "or";
foreach ($expr as $key => $value) {
if ($key !== "nodeType" && deduct($value) === $search) return $search;
}
// If that never happened, return the opposite (false for OR, true for AND)
return !$search;
}
使用第二个 JSON 作为输入的调用示例:
$json = '[{"nodeType": "or","0": {"nodeType": "and", "0": {"nodeType": "or", "0": {"nodeType": "and","1": true,"2": false},"3": true},"3": true},"2": {"nodeType": "or","4": false,"5": true}}]';
$obj = json_decode($json, true)[0]; // The outer level is an array: unwrap it.
var_dump(deduct($obj));
输出:
bool(true)