我需要在php中对多维数组进行重新排序,但是我无法正确处理它,而且我现在正在为foreach,for循环和for循环做斗争。
这就是我所拥有的:
Array
(
[dimension] => Array
(
[dimension.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => geheel
)
[1] => SimpleXMLElement Object
(
[0] => geheel
)
)
[dimension.type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => hoogte
)
[1] => SimpleXMLElement Object
(
[0] => breedte
)
)
[dimension.value] => Array
(
[0] => SimpleXMLElement Object
(
[0] => 73
)
[1] => SimpleXMLElement Object
(
[0] => 84
)
)
[dimension.unit] => Array
(
[0] => SimpleXMLElement Object
(
[0] => cm
)
[1] => SimpleXMLElement Object
(
[0] => cm
)
)
)
[material] => Array
(
[material.part] => Array
(
[0] => SimpleXMLElement Object
(
[0] => deelmateriaal
)
[1] => SimpleXMLElement Object
(
[0] => deelmateriaal2
)
)
[material_type] => Array
(
[0] => SimpleXMLElement Object
(
[0] => typemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => typemateriaal2
)
)
[material] => Array
(
[0] => SimpleXMLElement Object
(
[0] => materiaal
)
[1] => SimpleXMLElement Object
(
[0] => materiaal2
)
)
[material.notes] => Array
(
[0] => SimpleXMLElement Object
(
[0] => notemateriaal
)
[1] => SimpleXMLElement Object
(
[0] => notemateriaal2
)
)
)
)
我需要将其转换为:
dimensions => Array ( 1 => array(dimension.part => geheel)
=> array (dimension.type => hoogte) )
.....
=> Array ( 2 => array(dimension.part => ...)
=> array (dimension.type => ...) )
....
....
material => Array ( 1 => ...
2 => ...
=> Array ( 1 =>
=>
....
任何人都有这个好方法吗?
谢谢,
里斯
答案 0 :(得分:1)
foreach($arr['dimension'] as $keyType => $type) {
foreach($type as $key => $value) {
$aNewArray[$key][$keyType] = $value;
}
}
类似的东西..然后再处理它以满足您的需求..
它的作用:遍历每个维度数组(并将其键存储在$ keyType中),然后遍历每个项目并按照您希望的方式将其添加到新数组中。它假设每种类型的下面都有相同数量的项目。
答案 1 :(得分:0)
$new_array = new array();
$count = count($array['dimension']['dimension_part']);
for($i=0;$i<$count;$i++) {
$new_array[$i]['dimension.part'] = $array['dimension']['dimension.part'][$i][0];
$new_array[$i]['dimension.type'] = $array['dimension']['dimension.type'][$i][0];
$new_array[$i]['dimension.value'] = $array['dimension']['dimension.value'][$i][0];
}
我不太确定你如何访问simplexmlobjects,所以我使用了数组类型。可能会将其更改为[$i] -> 0