PHP: Search for a string in XML

后端 未结 2 1461
死守一世寂寞
死守一世寂寞 2021-01-24 01:57

I try the search code below but it is only showing the first child node. Is something missing from my code?

----catalog.xml----



        
2条回答
  •  一生所求
    2021-01-24 02:44

    I think this is what you want

    Category;
    $found = false;
    foreach($category as $c)
    {
        $menu = (string) ($c->Name);
        if ($menu == "CAT1" )
        {
            foreach ($c->Location as $loc)
            {
                echo $loc->Room;
            }
            $found = true; break;
        }
    }
    if (!$found)
    {
        echo "No result";
    }
    ?>
    

    Remove the break; statement if you can have more than one Category with the searched Name element.

提交回复
热议问题