SimpleXml数组到字符串PHP


SimpleXml array to string PHP

我的PHP函数返回一个SimpleXML数组,如下所示:

array(1) { [0]=> object(SimpleXMLElement)#6 (18) { ["@attributes"]=> array(6) { ["thumbnailUrl"]=> string(47) "users/908/721/swagg_ma_blue/thumbnail-12612.jpg" ["default"]=> string(1) "1" ["name"]=> string(5) "_copy" ["color"]=> string(8) "0xBB9977" ["mood"]=> string(1) "3" ["species"]=> string(4) "stud" } ["head"]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(4) { ["url"]=> string(51) "http://assets.zwinky.com/assets/stud/heads/01/head1" ["c"]=> string(8) "0xF2B38A" ["c2"]=> string(8) "0xffffff" ["z"]=> string(5) "33000" } } ["face"]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(5) { ["url"]=> string(51) "http://assets.zwinky.com/assets/stud/faces/01/stud1" ["c"]=> string(8) "0xFFFFFF" ["displayName"]=> string(5) "stud1" ["z"]=> string(5) "34000" ["id"]=> string(8) "20017217" } } ["belt"]=> array(2) { [0]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(6) { ["url"]=> string(64) "http://assets.zwinky.com/assets/store/zwinky/stud/belts/01/Vblt1" ["c"]=> string(8) "0x000000" ["c2"]=> string(8) "0xFFFFFF" ["displayName"]=> string(30) "Tough Love Belt ketamine-dolly" ["z"]=> string(5) "20100" ["id"]=> string(8) "20098984" } } [1]=> object(SimpleXMLElement)#20 (1) { ["@attributes"]=> array(5) { ["url"]=> string(76) "http://ak.ugc.zwinky.com/zwinky/fds/creations/791/402/z-justin/1441479408229" ["isUgc"]=> string(4) "true" ["displayName"]=> string(9) "My Design" ["z"]=> string(5) "20100" ["id"]=> string(8) "20081015" } } } ["midsection"]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(2) { ["url"]=> string(55) "http://assets.zwinky.com/assets/stud/midsections/01/ms1" ["z"]=> string(4) "9000" } } ["leg"]=> object(SimpleXMLElement)#10 (1) { ["@attributes"]=> array(2) { ["url"]=> string(50) "http://assets.zwinky.com/assets/stud/legs/01/legs1" ["z"]=> string(5) "10000" } } ["hair"]=> object(SimpleXMLElement)#11 (1) { ["@attributes"]=> array(6) { ["url"]=> string(62) "http://assets.zwinky.com/assets/store/zwinky/stud/hair/01/Zhr8" ["c"]=> string(8) "0xD0C0B0" ["c2"]=> string(8) "0x000000" ["displayName"]=> string(7) "spiky 3" ["z"]=> string(5) "37000" ["id"]=> string(8) "20013196" } } ["shirt"]=> object(SimpleXMLElement)#12 (1) { ["@attributes"]=> array(5) { ["url"]=> string(63) "http://assets.zwinky.com/assets/store/zwinky/stud/tops/01/Vtop2" ["c"]=> string(8) "0x212121" ["displayName"]=> string(8) "tank top" ["z"]=> string(5) "21000" ["id"]=> string(8) "20012196" } } ["jacket"]=> object(SimpleXMLElement)#13 (1) { ["@attributes"]=> array(5) { ["url"]=> string(76) "http://ak.ugc.zwinky.com/zwinky/fds/creations/113/681/b4d_b0yy/1452451820582" ["isUgc"]=> string(4) "true" ["displayName"]=> string(4) "asdf" ["z"]=> string(5) "30000" ["id"]=> string(8) "20080934" } } ["bottom"]=> object(SimpleXMLElement)#14 (1) { ["@attributes"]=> array(6) { ["url"]=> string(67) "http://assets.zwinky.com/assets/store/zwinky/stud/bottoms/01/MXbm14" ["c"]=> string(8) "0xD6D6D6" ["c2"]=> string(8) "0xFFFFFF" ["displayName"]=> string(15) "Steampunk Pants" ["z"]=> string(5) "20000" ["id"]=> string(8) "20039549" } } ["electronic"]=> object(SimpleXMLElement)#15 (1) { ["@attributes"]=> array(5) { ["url"]=> string(64) "http://assets.zwinky.com/assets/store/zwinky/stud/misc/01/SDmis1" ["c"]=> string(8) "0x3366FF" ["displayName"]=> string(12) "game player2" ["z"]=> string(5) "45000" ["id"]=> string(8) "20017518" } } ["hat"]=> object(SimpleXMLElement)#16 (1) { ["@attributes"]=> array(5) { ["url"]=> string(49) "http://assets.zwinky.com/assets/stud/hats/01/hat6" ["c"]=> string(3) "0x0" ["displayName"]=> string(13) "baseball cap2" ["z"]=> string(5) "40000" ["id"]=> string(8) "20017403" } } ["earrings"]=> object(SimpleXMLElement)#17 (1) { ["@attributes"]=> array(4) { ["url"]=> string(52) "http://assets.zwinky.com/assets/stud/earrings/01/er2" ["displayName"]=> string(10) "gold hoops" ["z"]=> string(5) "39000" ["id"]=> string(8) "20017442" } } ["bracelet"]=> object(SimpleXMLElement)#18 (1) { ["@attributes"]=> array(6) { ["url"]=> string(67) "http://assets.zwinky.com/assets/store/zwinky/stud/bracelets/01/Zbr1" ["c"]=> string(8) "0xFFFFFF" ["c2"]=> string(8) "0xB5B5B5" ["displayName"]=> string(13) "Leather Cuffs" ["z"]=> string(5) "13000" ["id"]=> string(8) "20013217" } } ["necklace"]=> object(SimpleXMLElement)#19 (1) { ["@attributes"]=> array(5) { ["url"]=> string(68) "http://assets.zwinky.com/assets/store/zwinky/stud/necklaces/01/SDnk1" ["c"]=> string(8) "0xffffff" ["displayName"]=> string(14) "Snoop Necklace" ["z"]=> string(5) "23000" ["id"]=> string(8) "20012554" } } ["facialhair"]=> object(SimpleXMLElement)#21 (1) { ["@attributes"]=> array(5) { ["url"]=> string(69) "http://assets.zwinky.com/assets/store/zwinky/stud/facialhair/01/Vfhr1" ["c"]=> string(8) "0xffffff" ["displayName"]=> string(16) "Evil Cupid beard" ["z"]=> string(5) "38000" ["id"]=> string(8) "20012194" } } ["food"]=> object(SimpleXMLElement)#22 (1) { ["@attributes"]=> array(4) { ["url"]=> string(64) "http://assets.zwinky.com/assets/store/zwinky/stud/misc/01/HWmis7" ["displayName"]=> string(11) "mug w/steam" ["z"]=> string(5) "47000" ["id"]=> string(8) "20017604" } } ["facialdecorations"]=> object(SimpleXMLElement)#23 (1) { ["@attributes"]=> array(5) { ["url"]=> string(88) "http://ak.ugc.zwinky.com/zwinky/fds/creations/235/447/simsea-gdh0thtb1tzch/1426865152881" ["isUgc"]=> string(4) "true" ["displayName"]=> string(9) "My Design" ["z"]=> string(5) "42500" ["id"]=> string(8) "20081020" } } } }

我的功能如下:

function get_user_outfit(){
    $zwinky_login = $this->curl_post_login();
    if($zwinky_login){
        $user_outfit = $this->curl_post('http://outfits.zwinky.com/users/' . $this->user_directories() . '/outfits.xml', false, $this->cookie, true, false);
        $xml = simplexml_load_string($user_outfit);
        $element = $xml->xpath("//outfit[@default=1]");
        return $element;
    }else{
        return 'Could not login.';
    }
}

我找不到把它翻译成字符串的方法。

我一直在尝试以下内容http://php.net/manual/en/simplexmlelement.asxml.php这是行不通的。

有人有主意吗?

$xml->xpath(...)返回类型为SimpleXMLElement的节点数组。

有一些内置函数可以将其转换为字符串,尽管您必须循环遍历返回的节点。

例如

$nodes = $xml->path(...);
$str = '';
foreach($nodes as $node){
    $str .= $node->asXML();
}
return $str;

如果需要节点的值,可以在SimpleXMLElement对象上调用魔术方法__toString。例如echo $node['attribute'];echo $xml->path('//outfit[@default=1]/some/name')[0];

或者使用PHP DOM,它可以为您提供更多的控制和灵活性。