PHP数组 - 删除不需要的密钥+保留数据

时间:2013-11-02 14:56:44

标签: php xml arrays multidimensional-array

如何从xml节点中完全删除不需要的父级并保持数据完好无损?类似阵列移位的东西。 例如,我有一个数组

            [LineItems] => Array
                (
                    [0] => Array
                        (
                            [LineItem] => Array
                                (
                                    [Description] => iPhone
                                    [Quantity] => 1
                                    [UnitAmount] => 101.0000
                                    [AccountCode] => 200
                                    [TaxAmount] => 0.0000
                                    [LineAmount] => 101.0000
                                )

                        )

                    [1] => Array
                        (
                            [LineItem] => Array
                                (
                                    [Description] => Flat Shipping Rate
                                    [Quantity] => 1
                                    [AccountCode] => 200
                                    [UnitAmount] => 5.0000
                                    [LineAmount] => 5.0000
                                )

                        )

                )

如何完全删除LineItems [0]和LineItems [1]并将LineItem数组推到左侧?我正在寻找这样的东西:

  [LineItems] => Array
                (
                    [LineItem] => Array
                                (
                                    [Description] => iPhone
                                    [Quantity] => 1
                                    [UnitAmount] => 101.0000
                                    [AccountCode] => 200
                                    [TaxAmount] => 0.0000
                                    [LineAmount] => 101.0000
                                )

                        )

                   [LineItem] => Array
                                (
                                    [Description] => Flat Shipping Rate
                                    [Quantity] => 1
                                    [AccountCode] => 200
                                    [UnitAmount] => 5.0000
                                    [LineAmount] => 5.0000
                                )

                        )

此外,转换数组时:

Array
(
    [Invoices] => Array
        (
......something....
)
)

转换为XML时,xml将输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <Invoices>
  </Invoices>
</root>

如何实际删除root?

提前致谢。

2 个答案:

答案 0 :(得分:1)

$tmparr=[];
array_push($tmparr,$arr[LineItems][0][LineItem],$arr[LineItems][1][LineItem]);
unset($arr[LineItems][0],$arr[LineItems][1]);
foreach($tmparr as $key=>$val){
$arr[LineItems][]=$val;
};
unset($tmparr);
我猜...

答案 1 :(得分:1)

foreach ($my_array['Invoices']['LineItems'] as & $lineItem) 
   $lineItem = $lineItem['LineItem'];

关于XML问题,您可以尝试此功能(基于a comment on PHP.net):

function array2xml($array, $root_element_name = 'root') 
{ 
    if (count($array) == 1) {
        $root_element_name = reset(array_keys($array));
        $array = reset($array);
    }
    $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}/>"); 

    function xml_builder($node, $array) { 
            foreach($array as $key => $value) { 
                if (is_array($value)) { 
                    if (is_integer($key) && count($value) == 1) {
                        $key = reset(array_keys($value));
                        $value = reset($value);
                    }
                    $child = $node->addChild($key); 
                    xml_builder(& $child, $value); 
                } else { 
                    $node->addChild($key, $value); 
                } 
            }
    }; 
    xml_builder(& $xml, $array); 

    return $xml->asXML(); 
} 

print htmlentities(array2xml($my_array));