Matthew Higgins
Matthew Higgins

Reputation: 608

SimpleXML Reading Key/Value Pairs

I'm sure something like this will have come up before, but I'm struggling to find what I'm looking for on here at all. I've read previous answers here lots of times, but never asked or answered myself before!

I'm using SimpleXML to read a response to an Amazon SimpleDB query, and the result is in the form;

<Item>
    <Name>Item 1</Name>
    <Attribute>
        <Name>Title</Name>
        <Value>Item 1 Title</Value>
    </Attribute>
    <Attribute>
        <Name>URL</Name>
        <Value>Item 1 URL</Value>
    </Attribute>
    <Attribute>
        <Name>Description</Name>
        <Value>Item 1 Description</Value>
    </Attribute>
    <Attribute>
        <Name>Site</Name>
        <Value>Item 1 Site</Value>
    </Attribute>
</Item>
<Item>
    <Name>Item 2</Name>
    <Attribute>
        <Name>Title</Name>
        <Value>Item 2 Title</Value>
    </Attribute>
    <Attribute>
        <Name>URL</Name>
        <Value>Item 2 URL</Value>
    </Attribute>
    <Attribute>
        <Name>Description</Name>
        <Value>Item 2 Description</Value>
    </Attribute>
    <Attribute>
        <Name>Site</Name>
        <Value>Item 2 Site</Value>
    </Attribute>
</Item>

I've got no problem reading the 'Name' at the top of the item, using foreach() in PHP to list all of them, and I have managed to do the same to create a list of 'Key/Value' pairs to cycle through listing.

I can't really figure out how to select, for example, a specific value of description, to place it where I want in a page.

Selecting the Item->Attribute gives an array in the form;

[Name] -> Title
[Value] -> Item 1 Title

And this is where I'm a bit stuck. Still within my existing foreach() cycling through the items, I want an array of the 'Attributes', so I can select, for example;

[Title] -> Item 1 Title
[URL] -> Item 1 URL

And so on.

I'm fine selecting the items and echoing their name, bit that's really as far as I get.

Since this is such a newbie question, I'm sure it's been asked or answered before, I can't see an answer anywhere else, so if anyone can point me to one, I'd be very grateful!

Thanks in Advance

Upvotes: 0

Views: 3244

Answers (2)

Mor&#242;Switie
Mor&#242;Switie

Reputation: 1516

In your foreach loop, create a new foreach loop to create an attribute array.

something like

<?php

foreach($items as $item)
{
    $attributes = array();

    foreach($item->Attribute as $attribute)
    {
        $attributes[$attribute->Name] = $attribute->Value;
    }

    echo 'Item name:'.$item->Name.'<br />';
    echo 'Item attribute URL:'.$attributes['URL'].'<br />';
    echo 'Item attribute Description:'.$attributes['Description'].'<br />';
}

?>

note: Not tested, adjust as needed

Upvotes: 1

topherg
topherg

Reputation: 4303

I tried this a while ago for twitter integration into a site.

I found a piece of code when searching for XML2ARRAY. As you can probably guess, this turns the XML data into a multi-dimensional array, which you can use a shed load of FOREACH's or just named array calls if you know the element title.

You can then get it to check that every time it hits an element called ATTRIBUTE, then it saves that data to a new array using the NAME as a key and the VALUE as the value, then just unsetting the ATTRIBUTE when done (for better memory handling) and setting a new key within that array of the new array of values.

Hope that helps

Upvotes: 0

Related Questions