如何循环一个未知深度的树数组并获取数组块?
How to loop a tree array with unknown depth and get array blocks?
我找到了循环并打印未知深度数组的解决方案:
printAllValues($arr);
function printAllValues($arr) {
if(!is_array($arr)) {
echo ($arr);
return;
}
foreach($arr as $k => $v) {
printAllValues($v);
}
}
但是这个解决方案单独打印每个数组值。我想立即获取 (id, name, is_master) 并保存到 db 中。不是每一个单独的。
要循环的数组如下所示:
Array
(
[data] => Array
(
[0] => Array
(
[id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[name] => xxxxxxx
[is_master] =>
[master_id] => ce93cab80820446290f7ea627578e6e9
[has_children] => 1
[nodes] => Array
(
[0] => Array
(
[id] => 431169710c83410d841ab3cce079a0fd
[name] => xxxxxxxxxxx
[is_master] =>
[master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[has_children] =>
[nodes] =>
)
[1] => Array
(
[id] => 98e15adfd57c4285981781590e112d32
[name] => xxxxxxxxx
[is_master] =>
[master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[has_children] => 1
[nodes] => Array
....
我想像这样循环并将每个块保存到 db:
[id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[name] => xxxxxxx
[is_master] =>
[master_id] => 1234567
将当前级别的值保存到数据库中。然后如果有children,循环children,递归调用函数
function saveAllValues($arr) {
save_to_db($arr['id'], $arr['name'], $arr['is_master'], $arr['master_id']);
if ($arr['has_children']) {
array_map('saveAllValues, $arr['nodes']);
}
}
如果你只想在遇到这些值时进行处理,那么你可以这样做:
function printAllValues($arr) {
if (!is_array($arr)) {
// don't process this
return;
}
if (
isset($arr["id"]) &&
isset($arr["name"]) &&
isset($arr["is_master"]) &&
isset($arr["master_id"])
) {
// all array keys exist
// process data
}
foreach ($arr as $k => $v) {
printAllValues($v);
}
}
如果您想检索所有值,我们可以进一步调整,return这些值的“简化”数组
function getAllValues($arr, $values = []) {
if (!is_array($arr)) {
// no modifications, return original array
return $values;
}
if (
isset($arr["id"]) &&
isset($arr["name"]) &&
isset($arr["is_master"]) &&
isset($arr["master_id"])
) {
// all array keys exist
// store data
$values[] = [
"id" => $arr["id"],
"name" => $arr["name"],
"is_master" => $arr["is_master"],
"master_id" => $arr["master_id"],
];
}
foreach ($arr as $k => $v) {
$values = getAllValues($v, $values);
}
return $values;
}
$simplified = getAllValues($arr);
echo json_encode($simplified);
/* Example Output
[
{
"id": "1",
"name": "1",
"is_master": "1",
"master_id": "1"
},
{
"id": "2",
"name": "2",
"is_master": "1",
"master_id": "2"
}
]
*/
NOTE: these are simple examples trying to match your code structure, there are many different ways to do this
我找到了循环并打印未知深度数组的解决方案:
printAllValues($arr);
function printAllValues($arr) {
if(!is_array($arr)) {
echo ($arr);
return;
}
foreach($arr as $k => $v) {
printAllValues($v);
}
}
但是这个解决方案单独打印每个数组值。我想立即获取 (id, name, is_master) 并保存到 db 中。不是每一个单独的。 要循环的数组如下所示:
Array
(
[data] => Array
(
[0] => Array
(
[id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[name] => xxxxxxx
[is_master] =>
[master_id] => ce93cab80820446290f7ea627578e6e9
[has_children] => 1
[nodes] => Array
(
[0] => Array
(
[id] => 431169710c83410d841ab3cce079a0fd
[name] => xxxxxxxxxxx
[is_master] =>
[master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[has_children] =>
[nodes] =>
)
[1] => Array
(
[id] => 98e15adfd57c4285981781590e112d32
[name] => xxxxxxxxx
[is_master] =>
[master_id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[has_children] => 1
[nodes] => Array
....
我想像这样循环并将每个块保存到 db:
[id] => 0ea8a4ab99694ce7b74f08ed7fc4602e
[name] => xxxxxxx
[is_master] =>
[master_id] => 1234567
将当前级别的值保存到数据库中。然后如果有children,循环children,递归调用函数
function saveAllValues($arr) {
save_to_db($arr['id'], $arr['name'], $arr['is_master'], $arr['master_id']);
if ($arr['has_children']) {
array_map('saveAllValues, $arr['nodes']);
}
}
如果你只想在遇到这些值时进行处理,那么你可以这样做:
function printAllValues($arr) {
if (!is_array($arr)) {
// don't process this
return;
}
if (
isset($arr["id"]) &&
isset($arr["name"]) &&
isset($arr["is_master"]) &&
isset($arr["master_id"])
) {
// all array keys exist
// process data
}
foreach ($arr as $k => $v) {
printAllValues($v);
}
}
如果您想检索所有值,我们可以进一步调整,return这些值的“简化”数组
function getAllValues($arr, $values = []) {
if (!is_array($arr)) {
// no modifications, return original array
return $values;
}
if (
isset($arr["id"]) &&
isset($arr["name"]) &&
isset($arr["is_master"]) &&
isset($arr["master_id"])
) {
// all array keys exist
// store data
$values[] = [
"id" => $arr["id"],
"name" => $arr["name"],
"is_master" => $arr["is_master"],
"master_id" => $arr["master_id"],
];
}
foreach ($arr as $k => $v) {
$values = getAllValues($v, $values);
}
return $values;
}
$simplified = getAllValues($arr);
echo json_encode($simplified);
/* Example Output
[
{
"id": "1",
"name": "1",
"is_master": "1",
"master_id": "1"
},
{
"id": "2",
"name": "2",
"is_master": "1",
"master_id": "2"
}
]
*/
NOTE: these are simple examples trying to match your code structure, there are many different ways to do this