我有一个xml
文件,我试图变成一个数组,但我希望键是索引,如果它们是一个数组,只需将键添加到当前数组,或者拆分每个数组成全局变量。一个例子如下。但是,由此我只能展平数组,我在https://github.com/mithereal/xml-to-struct-go
class RecursiveRealArrayIterator extends RecursiveArrayIterator
{
public function hasChildren()
{
$current = $this->current();
return is_array( $current ) || $current instanceof Traversable;
}
}
$xml = '<?xml version="1.0" encoding="utf-8"?>
<GetOrdersRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<!-- Call-specific Input Fields -->
<CreateTimeFrom> dateTime </CreateTimeFrom>
<CreateTimeTo> dateTime </CreateTimeTo>
<IncludeFinalValueFee> boolean </IncludeFinalValueFee>
<ListingType> ListingTypeCodeType </ListingType>
<ModTimeFrom> dateTime </ModTimeFrom>
<ModTimeTo> dateTime </ModTimeTo>
<NumberOfDays> int </NumberOfDays>
<OrderIDArray> OrderIDArrayType
<OrderID> OrderIDType (string) </OrderID>
<!-- ... more OrderID values allowed here ... -->
</OrderIDArray>
<OrderRole> TradingRoleCodeType </OrderRole>
<OrderStatus> OrderStatusCodeType </OrderStatus>
<Pagination> PaginationType
<EntriesPerPage> int </EntriesPerPage>
<PageNumber> int </PageNumber>
</Pagination>
<SortingOrder> SortOrderCodeType </SortingOrder>
<!-- Standard Input Fields -->
<DetailLevel> DetailLevelCodeType </DetailLevel>
<!-- ... more DetailLevel values allowed here ... -->
<ErrorLanguage> string </ErrorLanguage>
<MessageID> string </MessageID>
<OutputSelector> string </OutputSelector>
<!-- ... more OutputSelector values allowed here ... -->
<Version> string </Version>
<WarningLevel> WarningLevelCodeType </WarningLevel>
</GetOrdersRequest>'
$flat = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr)); return iterator_to_array($it, true);
function XMLToArray($xml)
{
$return = false;
if ($xml instanceof SimpleXMLElement) {
$children = $xml->children();
$return = false;
foreach ($children as $element => $value) {
if ($value instanceof SimpleXMLElement) {
$values = (array)$value->children();
if (count($values) > 0) {
$return[$element] = $this->XMLToArray($value);
} else {
if (!isset($return[$element])) {
$return[$element] = (string)$value;
} else {
if (!is_array($return[$element])) {
$return[$element] = array($return[$element], (string)$value);
} else {
$return[$element][] = (string)$value;
}
}
}
}
}
}
return $return;
}
class object(){
var properties
function newproperty($array, $i)
{
$this->properties[$i] = $array;
if (is_array($array)) {
$i++;
$this->newproperty($array,$i);
}
return $array;
}
function make($array)
{
$i = 0;
foreach ($array as $dim) {
$this->newproperty($dim, $i);
$i++;
}
return $this->properties;
}
}
$structs = obj::make($arr)
答案 0 :(得分:0)
您可以使用此功能获得如下结果
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<GetOrdersRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<!-- Call-specific Input Fields -->
<CreateTimeFrom> dateTime </CreateTimeFrom>
<CreateTimeTo> dateTime </CreateTimeTo>
<IncludeFinalValueFee> boolean </IncludeFinalValueFee>
<ListingType> ListingTypeCodeType </ListingType>
<ModTimeFrom> dateTime </ModTimeFrom>
<ModTimeTo> dateTime </ModTimeTo>
<NumberOfDays> int </NumberOfDays>
<OrderIDArray> OrderIDArrayType
<OrderID> OrderIDType (string) </OrderID>
<!-- ... more OrderID values allowed here ... -->
</OrderIDArray>
<OrderRole> TradingRoleCodeType </OrderRole>
<OrderStatus> OrderStatusCodeType </OrderStatus>
<Pagination> PaginationType
<EntriesPerPage> int </EntriesPerPage>
<PageNumber> int </PageNumber>
</Pagination>
<SortingOrder> SortOrderCodeType </SortingOrder>
<!-- Standard Input Fields -->
<DetailLevel> DetailLevelCodeType </DetailLevel>
<!-- ... more DetailLevel values allowed here ... -->
<ErrorLanguage> string </ErrorLanguage>
<MessageID> string </MessageID>
<OutputSelector> string </OutputSelector>
<!-- ... more OutputSelector values allowed here ... -->
<Version> string </Version>
<WarningLevel> WarningLevelCodeType </WarningLevel>
</GetOrdersRequest>';
//create xml object
$obj = simplexml_load_string($xml);
//object to array
$array = get_object_vars($obj);
print_r($array);