PHP dynamically creating associative array from XML -
so have xml i'm strolling through , i'm able move through fine. want dynamically create associative array in way:
$keyname => $valname
here's how 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 got code 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 $dict1){ foreach($dict1->children() $dictchild){ if($dictchild->getname() == "key"){ $keyname = $dictchild; } else { $valname = $dictchild; } } }
i've tried few things creating 2 arrays , try merge them...but fails me, have code incorrectly done.
essentially i'm going after 2nd foreach
loop completes drop data sql. need create associative first make work in codeigniter.
is looking for?
$myarray = array(); $currentkey; foreach($xml->dict[0] $dict1){ foreach($dict1->children() $dictchild){ if($dictchild->getname() == "key"){ $currentkey = $dictchild; } else { $myarray[(string)$currentkey] = (string)$dictchild; } } }
update tested out code , getting warning getting. casting strings seems fix that. below example file made , output 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] $dict1){ foreach($dict1->children() $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 )
Comments
Post a Comment