The task is to traverse the tree. For these purposes I use recursion. Now you need to remove an item by its link by condition.
class Test { public function __construct() { $this->test1 = [ 'a' => 1, 'b' => [ 'd' => [ 'e' => 'test' ], ], 'c' => null, 'd' => (object)[ 'e' => 'test', ], 'f' => (object)[ 'test' => 'a', 'e' => (object)[ 'test' => 'test', ], ] ]; $this->test2 = [ 'e' => 'test', ]; $this->test3 = 'test'; $this->checkTypeRecursive($this->test1); $this->checkTypeRecursive($this->test2); $this->checkTypeRecursive($this->test3); } public function checkTypeRecursive(&$values) { if (is_array($values)) { foreach ($values as $key => $value) { $this->checkTypeRecursive($values[$key]); } } elseif (is_object($values)) { foreach ($values as $key => $value) { $this->checkTypeRecursive($values->$key); } } else { if ($values == 'test') { //Присвоение по ссылке работает. $values = 'Привет'; /** А как удалить элемент на который эта ссылка получена */ //unset($values); //Так не сработает ибо это значение. /** * Остальная логика * .... */ } } } } print_r(new Test());
valueby reference? if you need to get a result, return it or as a good option to throw an exception to stop the recursion for sure and not suffer from returns - NaumovОстальная логикаnot just like that - Ninazu$keyfunction and dounset($values[$key])- Naumov