0

So I have this XML that I'm strolling through and I'm able to move through it fine. What I want to dynamically create is an associative array in this way:

$keyName => $valName

Here's how the XML looks like:

<dict>
    <key>Major Version</key><integer>1</integer>
    <key>Minor Version</key><integer>1</integer>
    <key>Application Version</key><string>7.6.1</string>
    <key>Tracks</key>
    <dict>
      <key>0</key>
      <dict>
        <key>Track ID</key><integer>0</integer>
        <key>Name</key><string>American Idol 2013</string>
        <key>Artist</key><string>Amber Holcomb</string>
        <key>Album Artist</key><string>Amber Holcomb</string>
        <key>Album</key><string>Unknown Album</string>
        <key>Kind</key><string>MPEG audio file</string>
        <key>Size</key><integer>3645</integer>
        <key>Total Time</key><integer>233000</integer>
        <key>Date Modified</key><date>Thu Mar 14 12:11:12 2013</date>
        <key>Date Added</key><date>Thu Apr 04 16:10:15 2013</date>
        <key>Bitrate</key><integer>128</integer>
        <key>Location</key><string>file://localhost/Z:%5Canthony%5CMusic%5CiTunes%5CiTunes%20Media%5CMusic%5CUnknown%20Artist%5CUnknown%20Album%5CAmber%20Holcomb%20-%20A%20Moment%20Like%20This%20-%20Studio%20Version%20-%20American%20Idol%202013.mp3</string>
        <key>File Folder Count</key><integer>-1</integer>
        <key>Library Folder Count</key><integer>-1</integer>
      </dict>

and here's what I got for my code so far:

$xml = simplexml_load_file(base_url().'uploads/xmlbackup2.xml');
        $varKey = $xml->dict[0]->dict[0]->dict[0]->children();

        $keyName ="";
        $valName ="";



foreach($xml->dict[0]->dict[0]->dict as $dict1){

            foreach($dict1->children() as $dictChild){

                if($dictChild->getName() == "key"){
                    $keyName = $dictChild;
                } else {
                    $valName = $dictChild;
                }



            }

        }

I've tried a few things like creating two arrays and try to merge them...but it just fails for me, most likely I have the code incorrectly done.

Essentially what I'm going to do after the 2nd foreach loop completes is to drop the data into SQL. However I need to create the associative first to make it work in codeigniter.

4
  • Do you use simpleXML ? Commented Apr 9, 2013 at 5:02
  • @svetlio - Yes, I updated my code to reflect that. Thanks. Commented Apr 9, 2013 at 5:06
  • php.net/manual/en/function.simplexml-load-string.php#102277 Try this .. it may help you Commented Apr 9, 2013 at 5:10
  • @Svetlio - Thanks, I saw that, but I don't know how that would work with the XML I have, its layout is weird looking to me and I don't know how it would associate the values in the KEY tags with the neighboring tags. For example: <key>Track ID</key><integer>0</integer> <key>Name</key><string>blah</string> Track ID => 0 Name => blah Commented Apr 9, 2013 at 5:14

1 Answer 1

2

Is this something like what you are looking for?

$myArray = Array();
$currentKey;    
foreach($xml->dict[0] as $dict1){
    foreach($dict1->children() as $dictChild){
        if($dictChild->getName() == "key"){
            $currentKey = $dictChild;
        } else {
            $myArray[(string)$currentKey] = (string)$dictChild;
        }
    }
}

UPDATE I tested out the code and I was getting the warning that you were getting. Casting to strings seems to fix that. Below is the example file I made and the output I am getting.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

$myArray = Array();
$currentKey;

$xml;
if (file_exists('test.xml'))
    $xml = simplexml_load_file('test.xml');

foreach($xml->dict[0] as $dict1){
    foreach($dict1->children() as $dictChild){
        if($dictChild->getName() == "key"){
            $currentKey = $dictChild;
        } else {
            $myArray[(string)$currentKey] = (string)$dictChild;
        }
    }
}

print '<pre>';
print_r($myArray);
print '</pre>';

?>

Array
(
    [Track ID] => 0
    [Name] => American Idol 2013
    [Artist] => Amber Holcomb
    [Album Artist] => Amber Holcomb
    [Album] => Unknown Album
    [Kind] => MPEG audio file
    [Size] => 3645
    [Total Time] => 233000
    [Date Modified] => Thu Mar 14 12:11:12 2013
    [Date Added] => Thu Apr 04 16:10:15 2013
    [Bitrate] => 128
    [Location] => file://localhost/Z:%5Canthony%5CMusic%5CiTunes%5CiTunes%20Media%5CMusic%5CUnknown%20Artist%5CUnknown%20Album%5CAmber%20Holcomb%20-%20A%20Moment%20Like%20This%20-%20Studio%20Version%20-%20American%20Idol%202013.mp3
    [File Folder Count] => -1
    [Library Folder Count] => -1
)
Sign up to request clarification or add additional context in comments.

3 Comments

Maybe this is it. Trying it now...brb
So I tried this, and I get: A PHP Error was encountered Severity: Warning Message: Illegal offset type Filename: controllers/xml.php Line Number: 22 -------- Line Number 22 is pointing to the $myArray[$currentKey] = $dictChild;
YUP YUP! Thank you very much for your help, that so worked! I tried this before, but never tried (string). thanks again! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.