Problem with WoWArmory SimpleXML attributes

That's an example:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [displayInfoId] => 62116
            [durability] => 100
            [gem0Id] => 41401
            [gem1Id] => 40123
            [gem2Id] => 0
            [gemIcon0] => inv_jewelcrafting_shadowspirit_02
            [gemIcon1] => inv_jewelcrafting_gem_37
            [icon] => inv_helmet_98
            [id] => 48592
            [level] => 245
            [maxDurability] => 100
            [name] => Liadrin Headpiece of Triumph
            [permanentEnchantIcon] => ability_warrior_shieldmastery
            [permanentEnchantItemId] => 44876
            [permanentenchant] => 3819
            [pickUp] => PickUpLargeChain
            [putDown] => PutDownLArgeChain
            [randomPropertiesId] => 0
            [rarity] => 4
            [seed] => 0
            [slot] => 0
        )

)

I am trying to get a JSON object with each element, but there is about 17 or something else, and if I try json_encode(), it will give me "@attributes" as an object containing everything I want. Help?

+3
source share
3 answers

How about this

$jsonArray = array();
foreach ($xmlObj->attributes() as $attr => $value) {
    $jsonArray[$attr] = (string)$value;
}

$jsonString = json_encode($jsonArray);

Edit: you can also just use

$jsonString = json_encode($xmlObj->attributes());

however, I'm not sure if the attribute values ​​are returned as strings or objects (editing - it turns out you can't. See Artefacto's solution).

+2
source

Something like that:

<?php
$sxm = new SimpleXMLElement("<a name=\"kkk\" other=\"foo\"/>");
$attrs = $sxm->attributes();
var_dump(json_encode(reset($attrs)));

gives:

string (28) "{" name ":" kkk "," other ":" foo "}"

, , , $xmlObj->attributes() SimpleXMLElement, "@attributes" , (name = > ).

+6

How about this one?

$array = (array)$simplexml->node->attributes();
$jsonArray = json_encode($array['@attributes']);
+1
source

Source: https://habr.com/ru/post/1772759/


All Articles