foreach ($decode as $k=>$item) {
if($item->rel == $id){
self::unsetData($item,$k);
return $item;
}
if($item->children){
foreach ($item->children as $k1=>$child) {
if($child->rel == $id){
self::unsetData($item,$k);
self::unsetData($child,$k1);
return $item;
}
if($child->children){
foreach ($child->children as $k2=>$ch) {
if($ch->rel == $id){
self::unsetData($item,$k1);
self::unsetData($child,$k2);
self::unsetData($ch,$k2);
return $item;
}
if($ch->children){
foreach ($ch->children as $k3=>$c) {
if($c->rel == $id){
self::unsetData($item,$k);
self::unsetData($child,$k1);
self::unsetData($ch,$k2);
return $item;
}
if($c->children){
foreach ($c->children as $k4=>$c2) {
if($c2->rel == $id){
self::unsetData($item,$k1);
self::unsetData($child,$k2);
self::unsetData($ch,$k3);
self::unsetData($c,$k4);
return $item;
}
}
}
}
}
}
}
}
}
}
function getItem($arr, $id, $result = null)
{
foreach($arr as $key => $item) {
if ($item->rel == $id) {
return $result ?: $item;
}
}
if ($item->children) {
return getItem($item->children, $id, $result ?: $item);
}
return null;
}
// use:
$foundItem = getItem($decode, 12);
function getItem($arr, $id, $result = null)
{
foreach($arr as $key => $item) {
if ($item->rel == $id) {
return $result ?: $item;
} else {
if (is_array($item->children)) {
$res = getItem($item->children, $id, $result ?: $item);
if ($res !== null) {
return $res;
}
}
}
}
return null;
}
// use:
$foundItem = getItem($data, 8);