简体   繁体   English

如何通过在分层键的循环数组中访问它来取消设置对象键 (php)

[英]How to unset object key by accessing it in loop array of hierarchical keys (php)

I have a XML Object我有一个 XML 对象

$xml = SimpleXMLElement Object
    (
        [a] => SimpleXMLElement Object
                    (
                        [b] => SimpleXMLElement Object
                                   (
                                       [c] => 'hey',
                                   ),
                     ),
    )

Now, I put all keys in array and I want to unset the last key ie 'c' from the object现在,我将所有键都放在数组中,我想取消设置最后一个键,即对象中的“c”

$temp = $xml;
$keys_arr = ['a', 'b', 'c'];
foreach($keys_arr => $key){
    $temp = $temp->$key;
}
unset($temp) // I want this to work like when we use this 'unset($xml->a->b->c);'

And when we print $temp:当我们打印 $temp 时:

print_r($temp);

the output should be:输出应该是:

SimpleXMLElement Object
        (
            [a] => SimpleXMLElement Object
                        (
                            [b] => SimpleXMLElement Object
                                       (
                                           // without '[c]' key
                                       ),
                         ),
        )

This is a recursive approach (as opposed to stacking):这是一种递归方法(与堆叠相反):

Code: ( Demo )代码:(演示

function recurse(&$object,$keys){  // modify the input by reference
    $key=array_shift($keys);
    if(isset($object->$key)){  // avoid trouble
        if(empty($keys)){
            unset($object->$key);  // remove at last key
        }else{
            recurse($object->$key,$keys);  // continue recursion
        }
    }
}

$xmlstring=<<<XML
    <root>
        <a>
            <b>
                <c>hey</c>
            </b>
        </a>
    </root>
XML;
$keys=['a','b','c'];

$xml=new SimpleXMLElement($xmlstring);
echo "Before: \n";
var_export($xml);
echo "\n----\nAfter:\n";

recurse($xml,$keys);  // modify input
var_export($xml);

Output:输出:

Before: 
SimpleXMLElement::__set_state(array(
   'a' => 
  SimpleXMLElement::__set_state(array(
     'b' => 
    SimpleXMLElement::__set_state(array(
       'c' => 'hey',
    )),
  )),
))
----
After:
SimpleXMLElement::__set_state(array(
   'a' => 
  SimpleXMLElement::__set_state(array(
     'b' => 
    SimpleXMLElement::__set_state(array(
    )),
  )),
))

And here is how I'd stack it to achieve the same result: Demo这是我如何堆叠它以达到相同的结果: Demo

$mod=&$xml;               // modify $xml by reference
$keys=['a','b','c'];
$last=array_pop($keys);   // assuming the array will not be empty, the array will contain valid/existing object keys
foreach($keys as $key){   // iterate/traverse all remaining keys (e.g. 'a','b' because 'c' was popped off)
    $mod=&$mod->$key;     // overwrite $mod by reference while traversing
}
unset($mod->$last);       // unset the last object
var_export($xml);         // print modified $xml to screen

Walk the keys array except last item, and finally do the unset.遍历除最后一项之外的键数组,最后执行取消设置。 But assign to $temp by reference!!但是通过引用分配给 $temp !!

$temp = &$xml;
$keys_arr = ['a', 'b', 'c'];
for($i,$size=count($keys_arr); $i<$size-1; $i++){
    $temp = &$temp->$keys_arr[$i];
}
unset($temp->$keys_arr[$i]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM