-1

I have a xml file that I am trying to turn into an array but I want the keys to be the indexes if they're an array else to just add the key to the current array, or split each array into a global variable. An example is below. However from this I can only flatten the array, I have more extensive code available at: 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)
6
  • Can you post your array structure with expected output Jan 19, 2016 at 7:00
  • if you don't have attributes then use simplexml library
    – Sundar
    Jan 19, 2016 at 7:04
  • they should be seperate arrays like something like this ... GetOrdersRequest[];GetOrdersRequest[CreateTimeFrom]; GetOrdersRequest[CreateTimeTo]; GetOrdersRequest[IncludeFinalValueFee]; GetOrdersRequest[ListingType]; GetOrdersRequest[WarningLevel]; ... OrderIDArray[OrderID]; .. Pagination[EntriesPerPage];
    – mithereal
    Jan 19, 2016 at 7:09
  • the goal is to turn the xml document into a c-like struct, so i need each array element that has a key that is also an array to be a seperate structure.
    – mithereal
    Jan 19, 2016 at 7:12
  • another way to say this is how to return all inclusive arrays as unique single diminsion arrays
    – mithereal
    Jan 19, 2016 at 7:44

1 Answer 1

0

You can use this function to get the result like below

<?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);
3
  • that doesnt work, it does not return correctly, it does output the first level correctly, then theres arrays i have to go into with loop logic, i want all the arrays in the collection/set to be put on 1 diminsion/plane i tried using tail recursion but it just blew up i added to the question
    – mithereal
    Jan 19, 2016 at 8:28
  • there is no otherway from my side you have to use loop only. This is not proper xml too <Pagination> PaginationType <EntriesPerPage> int </EntriesPerPage> <PageNumber> int </PageNumber> </Pagination> these details should be in attributes (PaginationType) or in tags.
    – Sundar
    Jan 19, 2016 at 9:08
  • you can directly use the object $obj->CreateTimeFrom until needs an array. only specific places you can use the loop
    – Sundar
    Jan 19, 2016 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.