0

所以我有这个我正在浏览的 XML,我可以很好地浏览它。我想以这种方式动态创建一个关联数组:

$keyName => $valName

下面是 XML 的样子:

<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>

到目前为止,这是我的代码得到的:

$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;
                }



            }

        }

我已经尝试了一些方法,例如创建两个数组并尝试合并它们......但它对我来说失败了,很可能我的代码错误地完成了。

foreach基本上,在第二个循环完成后我要做的是将数据放入 SQL 中。但是,我需要先创建关联以使其在 codeigniter 中工作。

4

1 回答 1

2

这是否像您正在寻找的东西?

$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;
        }
    }
}

更新 我测试了代码,我收到了你收到的警告。转换为字符串似乎可以解决这个问题。下面是我制作的示例文件和我得到的输出。

<?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
)
于 2013-04-09T05:06:55.590 回答