我有两个长度不同的嵌套数组。我要使长度相同。这些数组是嵌套数组,因此简单的array_slice无法正常工作。
这是数组的结构。
第一阵列
"id": "1",
"username": "username",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": [
{
"id": "-1",
"parent_id": "0",
"username": "NULL",
"position": "",
"children": []
}
]
}
]
}
]
}
}
第二阵列
"157": {
"id": "157",
"position": 1,
"username": "test1",
"parent_id": null,
"children": [
{
"id": "158",
"position": 1,
"parent_id": "157",
"username": "test1",
"children": [
{
"id": "159",
"position": 1,
"parent_id": "158",
"username": "test2",
"children": [
{
"id": "160",
"position": 1,
"parent_id": "159",
"username": "test3",
"children": []
}
]
}
]
},
{
"id": "160",
"position": 1,
"parent_id": "158",
"username": "test5",
"children": [
{
"id": "159",
"position": 1,
"parent_id": "158",
"username": "test2",
"children": [
{
"id": "161",
"position": 1,
"parent_id": "160",
"username": "test3",
"children": []
}
]
}
]
}
]
}
}
我正在尝试这种方法,但是没有用。
$firstCount = (array_map('count', $actualTree));
$secondCount = (array_map('count', $emptyTree));
$chunk = array_slice($actualTree, 0 , $second[$this->userId], true);
任何人都可以帮我使长度相同。任何帮助将不胜感激。 提前致谢。