<?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));
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
- array (
157 =>
array (
'id' => '157',
'username' => 'test1',
'children' =>
array (
0 =>
array (
'id' => '158',
'username' => 'test1',
'children' =>
array (
0 =>
array (
'id' => '159',
'username' => 'test2',
'children' =>
array (
0 =>
array (
'id' => '160',
'username' => 'test3',
'children' =>
array (
),
),
),
),
),
),
),
),
)
preferences:
137.35 ms | 409 KiB | 5 Q