du1462 2009-12-30 07:50
浏览 37
已采纳

在PHP中生成大量嵌套数组是不是很糟糕?

I want to transform the 'values' array created by xml_parse_into_struct() into a bunch of nested arrays which I can walk recursively. This is for a very simple XML class which will hierarchically search the document like so:

$xml_data = "
<sometag>
    <someothertag>
        <somedata>foo</somedata>
    </someothertag>
    <someothertag>
        <somedata>bar</somedata>
    </someothertag>
</sometag>
<sometag>
    <someothertag>
        <somedata>baz</somedata>
    </someothertag>
</sometag>";

$parser = new Xml_Data($xml_data);
$somedata = $parser->find('sometag')->find('someothertag')->results();
// 0: "somedata"
//    "value": "foo"
// 1: "somedata"
//    "value": "bar"

// etc.

storing it in nested associative arrays would make it much easier to work with than keeping track of each opening and closing tag and what 'level' they occur at like xml_parse_into_struct outputs. But I wonder -- if the document gets pretty big, will this huge array be horrible? Should I just give up and traverse the stupid version of the array that PHP gives me?

  • 写回答

3条回答 默认 最新

  • douze2890241475 2009-12-30 07:58
    关注

    For best effect you'd might wanna do

    $xml['sometag'][0]['someothertag'][1]['somedata'][0] # bar
    

    Where the array has two dimensions for each level. That is, pairs of tagname/tagindex values.

    This is both scalable and readable.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效