ОБНОВЛЕНИЕ: я переработал вопрос, чтобы показать прогресс, который я сделал, и, возможно, упростить ответ.
ОБНОВЛЕНИЕ 2: я добавил еще одно значение в XML.Расширение доступно в каждом почтовом индексе.Каждый элемент может иметь несколько элементов, разделенных вкладкой.Так что это будет структурировано так.Платформа> Расширение (подгруппа)> Имя> Заголовок.Если элемент имеет более одного расширения, он появится в нескольких местах.
У меня есть следующий XML-файл.
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/1/1.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/1/2.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif</Ext>
<Name>File Group 1</Name>
<Title>This is in the same group but has a different title</Title>
<DownloadPath>/this/windows/1/3.zip</DownloadPath>
</Item>
<Item>
<Platform>Mac</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.</Title>
<DownloadPath>/this/mac/1/1.zip</DownloadPath>
</Item>
<Item>
<Platform>Mac</Platform>
<Ext>jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.</Title>
<DownloadPath>/this/mac/1/2.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 2</Name>
<Title>This is the second file group</Title>
<DownloadPath>/this/windows/2/1.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 2</Name>
<Title>This is the second file group</Title>
<DownloadPath>/this/windows/2/2.zip</DownloadPath>
</Item>
<Item>
<Platform>Mac</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 3</Name>
<Title>This is the second mac file group really.</Title>
<DownloadPath>/this/windows/3/1.zip</DownloadPath>
</Item>
Я хочу иметь возможность просмотреть его и отсортироватьпоэтому я могу вставить его в схему нормализованной таблицы.Вот формат, который я хотел бы создать для массива.
[Windows] => Array (
[0] => array(
"Name" => "File Group 1",
"Title" => "This is the first file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/1/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 1",
"Title" => "This has the same name but has a different title, so it should be seperate.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/3.zip"
)
)
),
[1] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/2/2.zip"
)
)
)
),
[Mac] => Array(
[0] => array(
"Name" => "File Group 1",
"Title" => "This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/1/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 3",
"Title" => "This is the second mac file group really.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/1/2.zip"
)
)
),
)
Вот то, что я получил до сих пор с моим php
<code> $scrape_xml = "files.xml";
$xml = simplexml_load_file($scrape_xml);
$groups = array();
foreach ($xml->Item as $file){
if (!isset($groups[stripslashes($file->Platform)][stripslashes($file->Name)][stripslashes($file->Title)])){
$groups[stripslashes($file->Platform)][stripslashes($file->Name)][stripslashes($file->Title)] = array(
'Platform' => $file->Platform,
'Name' => $file->Name,
'Title' => $file->Title
);
}
$groups[stripslashes($file->Platform)][stripslashes($file->Name)][stripslashes($file->Title)]['Files'][] = $file->DownloadPath;
}
echo "count=" . $i;
echo "<pre>";
print_r($groups);
echo "
";
это дает мне этот результат
Array
(
[Windows] => Array
(
[File Group 1] => Array
(
[This is the first file group] => Array
(
[Platform] => SimpleXMLElement Object
(
[0] => Windows
)
[Name] => SimpleXMLElement Object
(
[0] => File Group 1
)
[Title] => SimpleXMLElement Object
(
[0] => This is the first file group
)
[Files] => Array
(
[0] => SimpleXMLElement Object
(
[0] => /this/windows/1/1.zip
)
[1] => SimpleXMLElement Object
(
[0] => /this/windows/1/2.zip
)
)
)
[This is in the same group but has a different title] => Array
(
[Platform] => SimpleXMLElement Object
(
[0] => Windows
)
[Name] => SimpleXMLElement Object
(
[0] => File Group 1
)
[Title] => SimpleXMLElement Object
(
[0] => This is in the same group but has a different title
)
[Files] => Array
(
[0] => SimpleXMLElement Object
(
[0] => /this/windows/1/3.zip
)
)
)
)
[File Group 2] => Array
(
[This is the second file group] => Array
(
[Platform] => SimpleXMLElement Object
(
[0] => Windows
)
[Name] => SimpleXMLElement Object
(
[0] => File Group 2
)
[Title] => SimpleXMLElement Object
(
[0] => This is the second file group
)
[Files] => Array
(
[0] => SimpleXMLElement Object
(
[0] => /this/windows/2/1.zip
)
[1] => SimpleXMLElement Object
(
[0] => /this/windows/2/2.zip
)
)
)
)
)
[Mac] => Array
(
[File Group 1] => Array
(
[This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.] => Array
(
[Platform] => SimpleXMLElement Object
(
[0] => Mac
)
[Name] => SimpleXMLElement Object
(
[0] => File Group 1
)
[Title] => SimpleXMLElement Object
(
[0] => This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.
)
[Files] => Array
(
[0] => SimpleXMLElement Object
(
[0] => /this/mac/1/1.zip
)
[1] => SimpleXMLElement Object
(
[0] => /this/mac/1/2.zip
)
)
)
)
[File Group 3] => Array
(
[This is the second mac file group really.] => Array
(
[Platform] => SimpleXMLElement Object
(
[0] => Mac
)
[Name] => SimpleXMLElement Object
(
[0] => File Group 3
)
[Title] => SimpleXMLElement Object
(
[0] => This is the second mac file group really.
)
[Files] => Array
(
[0] => SimpleXMLElement Object
(
[0] => /this/windows/3/1.zip
)
)
)
)
)
)
ОБНОВЛЕНИЕ 2: Новая структура массива
[Windows] => Array (
[gif] =>Array(
[0] => array(
"Name" => "File Group 1",
"Title" => "This is the first file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/1/2.zip"
)
)
)
),
[jpeg] => array(
[0] => array(
"Name" => "File Group 1",
"Title" => "This is the first file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/1/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/2/2.zip"
)
)
)
),
[doc] => array(
[0] => array(
"Name" => "File Group 1",
"Title" => "This is the first file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/1/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 1",
"Title" => "This has the same name but has a different title, so it should be seperate.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/1/3.zip"
)
)
),
[2] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/windows/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/windows/2/2.zip"
)
)
)
)
),
[Mac] => Array(
[gif] => array(
[0] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/2/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/2/2.zip"
)
)
),
)
[jepg] => array(
[0] => array(
"Name" => "File Group 2",
"Title" => "This is the second file group",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/2/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/2/2.zip"
)
)
)
)
[doc] => array(
[0] => array(
"Name" => "File Group 1",
"Title" => "This has the same group name but a different platform. Because it has the same title and name the files are added to this array below.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/1/2.zip"
)
)
),
[1] => array(
"Name" => "File Group 3",
"Title" => "This is the second mac file group really.",
"Files" => array(
[0] => array(
"DownloadPath" => "/this/mac/1/1.zip"
),
[1] => array(
"DownloadPath" => "/this/mac/1/2.zip"
)
)
)
)
)
ОБНОВЛЕНИЕ 3: В список файлов поступает мусор.
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/1/1.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/1/2.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/2/1.zip</DownloadPath>
</Item>
<Item>
<Platform>Windows</Platform>
<Ext>gif jpeg doc</Ext>
<Name>File Group 1</Name>
<Title>This is the first file group</Title>
<DownloadPath>/this/windows/2/2.zip</DownloadPath>
</Item>
Существует элемент с той же платформой, расширениями, именем и названием. Элементы 3 и 4 выше необходимо пропустить и сохранить их в массив, который я буду обрабатывать позже.