Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6090933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:16:13+00:00 2026-05-23T12:16:13+00:00

Can anyone help with converting data from an XML document into an associative array?

  • 0

Can anyone help with converting data from an XML document into an associative array? I’m running into issues given that the XML structure is sort of 3D and the array is more of a 2D structure (please forgive my lack of correct terminology throughout).
The XML elements have attributes, children and grand-children (but I never know their names), so I figured I’d try to make the key in the array a concatenation of each child/attribute name and the value equal to, well, the value. Trouble is I need the attribute name and value as part of the concatenated array key to make it unique…

For example:

<Computer id="1">   
    <OS>
        <Name>Linux</Name>
        <Age>Older than me</Age>
    </OS>
</Computer>
<Computer id="2">
    <OS>
        <Name>Windows</Name>
        <Age>Not so much</Age>
    </OS>
</Computer>

Should ideally give:

[Computer-id-1-OS-Name] = 'Linux'
[Computer-id-1-OS-Age] = 'Older than me'
[Computer-id-2-OS-Name] = 'Windows'
[Computer-id-2-OS-Age] = 'Not so much'

But I’m getting this result:

[Computer-id] = '1'
[Computer-OS-Name] = 'Linux'
[Computer-OS-Age] = 'Older than me'
[Computer-id] = '2'
[Computer-OS-Name] = 'Windows'
[Computer-OS-Age] = 'Not so much'

So that the [Computer-id] key is not unique. I’m using a recursive function to read in the values, but I can’t figure how to get the attribute name and attribute value into the name of the subordinate keys…(By the way there is a good reason for doing this seemingly illogical task!)
Any help would be greatly appreciated…

Here is the function which ‘flattens’ the XML data after it has been read into a multi-dimensional array. I’m not sure I’m going about this the right way!

function flattenArray ($array, $baseName = NULL)
{
    reset($array);
    while (list ($key, $value) = each($array)) {
        $outKey = $key . "-";
        if (is_array($value)) {
            flattenArray($value, $baseName . $outKey);
        } else {
            $finalKey = $baseName . rtrim($outKey, '-');
            $finalValue = $value;
            echo "$finalKey = $finalValue\n";
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:16:13+00:00Added an answer on May 23, 2026 at 12:16 pm

    One example could be:

    $dom = new DOMDocument;
    $dom->loadXML(
        '<root>
            <Computer id="1">   
                <OS>
                    <Name>Linux</Name>
                    <Age>Older than me</Age>
                </OS>
            </Computer>
    
            <Computer id="2">
                <OS>
                    <Name>Windows</Name>
                    <Age>Not so much</Age>
                </OS>
            </Computer>
        </root>'
    );
    
    $xpath = new DOMXPath($dom);
    $result = array();
    
    foreach ($xpath->query('//*[count(*) = 0]') as $node) {
        $path = array();
        $val = $node->nodeValue;
    
        do {
            if ($node->hasAttributes()) {
                foreach ($node->attributes as $attribute) {
                    $path[] = sprintf('%s[%s]', $attribute->nodeName, $attribute->nodeValue);
                }
            }
            $path[] = $node->nodeName;
        }
        while ($node = $node->parentNode);
    
        $result[implode('/', array_reverse($path))] = $val;
    }
    
    print_r($result);
    

    Output:

    Array
    (
        [#document/root/Computer/id[1]/OS/Name] => Linux
        [#document/root/Computer/id[1]/OS/Age] => Older than me
        [#document/root/Computer/id[2]/OS/Name] => Windows
        [#document/root/Computer/id[2]/OS/Age] => Not so much
    )
    

    Thats not exactly what you’re looking for, but it’s a start and can easily be tweaked to give different results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK.. I'm stuck. Can someone help me converting this JQL SELECT a FROM Asset
Can anyone help me with a way of converting String to double in vc++?
Can anyone help me with the trying to write SQL (MS SqlServer) - I
I just forgot the password. Can anyone help me how to get back the
Wondering if anyone can help me with this annoying but trivial (in terms of
I was wondering if anyone can help me get started with creating a room
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can anyone suggest some good browser add-on tools/extensions to help with development? I have
Can anyone recommend a good resource -- book, website, article, etc -- to help
Are there some help resources, or can anyone give me a brief Idea how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.