Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP dynamically creating associative array from XML
    primarykey
    data
    text
    <p>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:</p> <pre><code>$keyName =&gt; $valName </code></pre> <p>Here's how the XML looks like:</p> <pre><code>&lt;dict&gt; &lt;key&gt;Major Version&lt;/key&gt;&lt;integer&gt;1&lt;/integer&gt; &lt;key&gt;Minor Version&lt;/key&gt;&lt;integer&gt;1&lt;/integer&gt; &lt;key&gt;Application Version&lt;/key&gt;&lt;string&gt;7.6.1&lt;/string&gt; &lt;key&gt;Tracks&lt;/key&gt; &lt;dict&gt; &lt;key&gt;0&lt;/key&gt; &lt;dict&gt; &lt;key&gt;Track ID&lt;/key&gt;&lt;integer&gt;0&lt;/integer&gt; &lt;key&gt;Name&lt;/key&gt;&lt;string&gt;American Idol 2013&lt;/string&gt; &lt;key&gt;Artist&lt;/key&gt;&lt;string&gt;Amber Holcomb&lt;/string&gt; &lt;key&gt;Album Artist&lt;/key&gt;&lt;string&gt;Amber Holcomb&lt;/string&gt; &lt;key&gt;Album&lt;/key&gt;&lt;string&gt;Unknown Album&lt;/string&gt; &lt;key&gt;Kind&lt;/key&gt;&lt;string&gt;MPEG audio file&lt;/string&gt; &lt;key&gt;Size&lt;/key&gt;&lt;integer&gt;3645&lt;/integer&gt; &lt;key&gt;Total Time&lt;/key&gt;&lt;integer&gt;233000&lt;/integer&gt; &lt;key&gt;Date Modified&lt;/key&gt;&lt;date&gt;Thu Mar 14 12:11:12 2013&lt;/date&gt; &lt;key&gt;Date Added&lt;/key&gt;&lt;date&gt;Thu Apr 04 16:10:15 2013&lt;/date&gt; &lt;key&gt;Bitrate&lt;/key&gt;&lt;integer&gt;128&lt;/integer&gt; &lt;key&gt;Location&lt;/key&gt;&lt;string&gt;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&lt;/string&gt; &lt;key&gt;File Folder Count&lt;/key&gt;&lt;integer&gt;-1&lt;/integer&gt; &lt;key&gt;Library Folder Count&lt;/key&gt;&lt;integer&gt;-1&lt;/integer&gt; &lt;/dict&gt; </code></pre> <p>and here's what I got for my code so far:</p> <pre><code>$xml = simplexml_load_file(base_url().'uploads/xmlbackup2.xml'); $varKey = $xml-&gt;dict[0]-&gt;dict[0]-&gt;dict[0]-&gt;children(); $keyName =""; $valName =""; foreach($xml-&gt;dict[0]-&gt;dict[0]-&gt;dict as $dict1){ foreach($dict1-&gt;children() as $dictChild){ if($dictChild-&gt;getName() == "key"){ $keyName = $dictChild; } else { $valName = $dictChild; } } } </code></pre> <p>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.</p> <p>Essentially what I'm going to do after the 2nd <code>foreach</code> loop completes is to drop the data into SQL. However I need to create the associative first to make it work in codeigniter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload