我想从xml中提取每个“ Marca”的详细信息。我的阵列应该为每个“ Marca”使用所有的“ Diametru”,所有的“ Latime”,所有的“ PCD”等。我正试图那样做,但是还是有问题。...我会很感激任何建议:
<?php
$jante = "http://vilavaleaprahovei.ro/kimea/feeds/alcarRO_wheels_feed.xml";
$xml=simplexml_load_file($jante);
$arrayName =array();
foreach($xml->Produs as $child)
{
$arrayName['marca'] = (string)$child->Marca;
if ($arrayName['marca'] == (string)$child->Marca){
$arrayName['marca']['diametru'][] = (int)$child->Diametru;
$arrayName['marca']['latime'][] = (int)$child->Latime;
$arrayName['marca']['pcd'][] = (int)$child->PCD;
$arrayName['marca']['pcd1'][] = (int)$child->PCD1;
$arrayName['marca']['et'][] = (int)$child->ET;
$arrayName['marca']['cb'][] = (int)$child->CB;
}
}
$arrayName['marca'] = array_unique($arrayName['marca']);
$arrayName['marca']['diametru'] = array_unique($arrayName['diametru']);
$arrayName['marca']['latime'] = array_unique($arrayName['latime']);
$arrayName['marca']['pcd'] = array_unique($arrayName['pcd']);
$arrayName['marca']['pcd1'] = array_unique($arrayName['pcd1']);
$arrayName['marca']['et'] = array_unique($arrayName['et']);
$arrayName['marca']['cb'] = array_unique($arrayName['cb']);
print_r($arrayName);
所需输出结构的示例:
Array
(
[marca] => AEZ
(
[diametru] => Array
(
[0] => 15
[2] => 16
[3] => 17
........
)
[latime] => Array
(
[0] => 6
[3] => 7
[31] => 5
........
)
[pcd] => Array
(
[0] => 4
[1] => 5
[67] => 6
........
)
[pcd1] => Array
(
[0] => 100
[1] => 112
[2] => 114
........
)
[et] => Array
(
[0] => 40
[1] => 47
[2] => 48
........
)
[cb] => Array
(
[0] => 60
[1] => 57
[2] => 72
........
)
)
[marca] => ALCAR STAHLRAD
(
[diametru] => Array
(
[0] => 10
[2] => 22
[3] => 2
........
)
[latime] => Array
(
[0] => 6
[3] => 3
[31] => 5
........
)
[pcd] => Array
(
[0] => 7
[1] => 5
[67] => 8
........
)
[pcd1] => Array
(
[0] => 90
[1] => 11
[2] => 114
........
)
[et] => Array
(
[0] => 40
[1] => 48
[2] => 48
........
)
[cb] => Array
(
[0] => 7
[1] => 57
[2] => 72
........
)
)
[marca] => ......
....................
答案 0 :(得分:0)
我认为您想要这样的东西:
<?php
$jante = "http://vilavaleaprahovei.ro/kimea/feeds/alcarRO_wheels_feed.xml";
$xml=simplexml_load_file($jante);
$items = []; // prepare main array
foreach($xml->Produs as $child)
{
// collect details of this current item:
$item = [];
$item['diametru'] = (int)$child->Diametru;
$item['latime'] = (int)$child->Latime;
$item['pcd'] = (int)$child->PCD;
$item['pcd1'] = (int)$child->PCD1;
$item['et'] = (int)$child->ET;
$item['cb'] = (int)$child->CB;
// add this item to the main array, structured by 'marca'
$marca = (string)$child->Marca;
$items[$marca][] = $item;
}
var_dump($items);
编辑:
这是获得所需结构的新版本:
foreach($xml->Produs as $child)
{
$marca = (string)$child->Marca;
if(!isset($items[$marca])) {
$items[$marca] = [];
}
$items[$marca]['diametru'][] = (int)$child->Diametru;
$items[$marca]['latime'][] = (int)$child->Latime;
$items[$marca]['pcd'][] = (int)$child->PCD;
$items[$marca]['pcd1'][] = (int)$child->PCD1;
$items[$marca]['et'][] = (int)$child->ET;
$items[$marca]['cb'][] = (int)$child->CB;
}
// EDIT 2:
// to make the entries unique you could loop through marcas
foreach($items as $marca => $item) {
$items[$marca]['diametru'] = array_unique($items[$marca]['diametru']);
$items[$marca]['latime'] = array_unique($items[$marca]['latime']);
// repeat for other fields...
}