douwei1904 2018-07-30 17:01
浏览 74

PHP /从Array中提取导航

I have an XML document and i'm using SimpleXMLElement to parse it with PHP like :

<Document>
     <Hello>
         <Name>Jason</Name>
    </Hello>
</Document>

Example to access to the name loading my XML and then i do :

$xml->Document->Hello->Name

I would like to store all this routes in associative array like

$array = [
   "Document->Hello->Name" => "name"
];

The problem is when i loop on this array my field is empty I do this :

foreach($array as $key => $v)
{
  $hereIsempty = $xml->$key
}

Is someone have a solution to get the value i want from my array mapping plz

  • 写回答

1条回答 默认 最新

  • down100009 2018-08-21 18:02
    关注

    Storing full route path in one key is a bad idea. if you know in advance the meaning of the path: set the tags name to const value and then use it.

    But if you realy need to save path as key in array, you can separate all tags in first sub-array and set needs value as second element of array; something like this:

    $needs = [];
    
    $routes = [
        [
            'tags' => ['Document', 'Hello'], 
            'need' => 'Name'
        ]
    ];
    
    foreach ($routes as $route) {
        tempXml = $xml;
    
        foreach ($route['tags'] as $tag) {
            $tempXml = $tempXml->{$tag};
        }
    
        $need[] = (string)$tempXml->{$route['need']};
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向