<?php
$structure = [
"1" => [
'id' => '1',
'username' => 'username',
'children' => [
[
'id' => '-1',
'username' => 'NULL',
'children' => [
[
'id' => '-1',
'username' => 'NULL',
'children' => [
[
'id' => '-1',
'username' => 'NULL',
'children' => [],
],
],
],
],
],
],
],
];
$entry = [
157 => [
'id' => '157',
'username' => 'test1',
'children' => [
[
'id' => '158',
'username' => 'test1',
'children' => [
[
'id' => '159',
'username' => 'test2',
'children' => [
[
'id' => '160',
'username' => 'test3',
'children' => [],
],
[
'id' => '160',
'username' => 'KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY',
'children' => [],
],
],
],
],
],
[
'id' => '160',
'username' => 'KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY',
'children' => [
[
'id' => '159',
'username' => 'KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY',
'children' => [
[
'id' => '161',
'username' => 'KICK ME BECAUSE I M NOT EXIST IN FIRST ARRAY',
'children' => [],
],
],
],
],
],
],
],
];
function truncateRecursive($structure, $entry) {
$output = [];
while (($structureKey = key($structure)) !== null && ($entryKey = key($entry)) !== null) {
$output[$entryKey] = !is_array($entry[$entryKey])
? $entry[$entryKey]
: truncateRecursive($structure[$structureKey], $entry[$entryKey]);
unset($structure[$structureKey], $entry[$entryKey]);
}
return $output;
}
var_export(truncateRecursive($structure, $entry));
preferences:
123.1 ms | 405 KiB | 5 Q