doujia4619 2013-08-15 06:34 采纳率: 0%
浏览 89
已采纳

PHP和XML - 在文档中设置唯一ID

i have this script example:

while (count($xml->xpath('*[@id="' . $id . '"]/@id')) > 0)
{
    $id = 'example_' . ++$x;
}

But if i check this with 1.000 Childrens in the element, i must wait more than a hour for an response.

How i could quicker check if a id exists?

Thank's in Advance for your Tip!

  • 写回答

2条回答 默认 最新

  • donglan9517 2013-08-16 00:55
    关注

    Thanks @michi:

    I modified the example and this is the final answer:

    $ids = $xml->xpath('*[@id="' . $id . '"]/@id');
    if(count($ids) > 0)
    {
        $ids = $xml->xpath('*[starts-with(@id, "example_")]/@id');
        $id = max(array_map(
                function($a)
                {
                    list(, $id) = explode('_', $a);
                    return intval($id);
                }, $ids)) + 1;
        $id = 'example_' . $id;
    }
    

    =}

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部