php array replace upto same depth

I'm building a binary tree where I have two arrays. First array has empty tree structure and second array has actual values. Below are the structure of both arrays.
First Array
"157": {
"id": "157",
"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": []
}
]
}
]
}
]
}
}
Second Array
"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": []
}
]
}
]
}
]
}
}
I'm usingarray_replace_recursive
function to replace empty values with actual value. It is working fine when both the arrays have same depth. For example if first array has 3 depth and second array also has 3 depth then it is working fine, but if second array has more values then first array then it is breaking array tree structure. So now I want to replace values upto depth of first array, no matter how big is second array, just replace values upto the depth first array.
Is this really possible?
Thank you!
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.