Prashanth Prashanth - 2 months ago 8
PHP Question

How to create numerical indexed array to XML?

I am working on array to XML conversion using PHP. I am using the following code:

function array_to_xml($template_info, &$xml_template_info) {
foreach($template_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){

$subnode = $xml_template_info->addChild("$key");

if(count($value) >1 && is_array($value)){
$jump = false;
$count = 1;
foreach($value as $k => $v) {
if(is_array($v)){
if($count++ > 1)
$subnode = $xml_template_info->addChild("$key");

array_to_xml($v, $subnode);
$jump = true;
}
}
if($jump) {
goto LE;
}
array_to_xml($value, $subnode);
}
else
array_to_xml($value, $subnode);
}
else{
array_to_xml($value, $xml_template_info);
}
}
else {
$xml_template_info->addChild("$key","$value");
}

LE: ;
}
}


but the problem is I can't able to create numerical standard value for
sub node
which is having
numerical array
value,

I am trying to achieve the following method.

Array
(
[DateTimeStamp] => 06/30/2017 08:11:23
[Sender] => Array
(
[SenderID] => TRN
[SenderName] => Transportation Reservation System,Inc.
)

[Recipient] => Array
(
[RecipientID] => DATA1
[RecipientName] =>
)
[Payload] => Array
(
[RateProduct] => Array
(
[0] => Array
(
[RateVendor] => 42074
[RateId] => 13
[RentalLocationID] => 262
[RateCompanyID] => RCR
[TotalPricing] => Array
(
[RentalDays] => 1
[RateCharge] => 127.50
)
[Taxes] => Array
(
[Tax2Amount] => 154.04
[Tax2Rate] => 0.13
)
[0] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)

)

[1] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)

)
)
[1] => Array
(
[RateVendor] => 42074
[RateId] => 13
[RentalLocationID] => 262
[RateCompanyID] => RCR
[TotalPricing] => Array
(
[RentalDays] => 1
[RateCharge] => 127.50
)
[Taxes] => Array
(
[Tax2Amount] => 154.04
[Tax2Rate] => 0.13
)
[0] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)

)

[1] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)

)
)
)
)
)


this
array
value need to be changed to the following format

<?xml version="1.0" encoding="UTF-8"?>
<PRE>
<TRNXML Version="1.0.0" TimeZone="ET" />
<DateTimeStamp>06/30/2017 04:36:04</DateTimeStamp>
<Sender>
<SenderID>TRN</SenderID>
<SenderName>Transportation Reservation System,Inc.</SenderName>
</Sender>
<Recipient>
<RecipientID>ROUTES</RecipientID>
<RecipientName />
</Recipient>
<Payload>
<RateProduct>
<RateVendor>FCAR</RateVendor>
<RateID />
<RentalLocationID>FCAR</RentalLocationID>
<RateCompanyID>FCAR</RateCompanyID>
<TotalPricing>
<RentalDays>FCAR</RentalDays>
<RateCharge>FCAR</RateCharge>
</TotalPricing>
<Taxes>
<Tax2Amount>FCAR</Tax2Amount>
<Tax2Rate>FCAR</Tax2Rate>
</Taxes>
///this has to come in following format
<DailyExtra>
<ExtraCode>FCAR</ExtraCode>
<ExtraDesc>FCAR</ExtraDesc>
</DailyExtra>
<DailyExtra>
<ExtraCode>FCAR</ExtraCode>
<ExtraDesc>FCAR</ExtraDesc>
</DailyExtra>
</RateProduct>
</Payload>
</PRE>


How to make it work in the XML code?

Answer Source

check it:

#declare
/**
 * $array - IN array
 * $dom - DOMDocument object
 * $node - DomElement
 * 
 * */
function array_to_xml($array, &$dom, &$node){
    foreach($array as $key=>$value){
        if( is_array( $value ) ){
            if ( is_numeric($key) ){
                array_to_xml($value, $dom, $node);
            }
            elseif( is_string($key) ){
                $el = $dom->createElement($key);
                array_to_xml($value, $dom, $el);
                $node->appendChild($el);
            }
        }
        elseif ( empty($value) ){
            $el = $dom->createElement($key);
            $node->appendChild($el);
        }
        elseif( is_string($key) && is_string($value)){
            $el = $dom->createElement($key, $value);
            $node->appendChild($el);
        }
    }
}


#implementation

    $doc = new DOMDocument('1.0', 'UTF-8');

    $doc->formatOutput = true;

    $pre = $doc->createElement('PRE');


    #<TRNXML Version="1.0.0" TimeZone="ET" />
    $TRNXML = $doc->createElement('TRNXML');
    $TRNXML->setAttribute('Version', '1.0.0');
    $TRNXML->setAttribute('TimeZone', 'ET');
    $pre->appendChild($TRNXML);

    $array = array (
            'DateTimeStamp' => '06/30/2017 08:11:23',
            'Sender' => array   (
                    'SenderID' => 'TRN',
                    'SenderName' => 'Transportation Reservation System,Inc.'),
            'Recipient' => array(
                    'RecipientID' => 'DATA1',
                    'RecipientName' => ''   ),
            'Payload' => array (
                    'RateProduct' => array(
                            0 => array(
                                    'RateVendor' => 42074,
                                    'RateId' => 13,
                                    'RentalLocationID' => 262,
                                    'RateCompanyID' => 'RCR',
                                    'TotalPricing' => array(
                                            'RentalDays' => 1,
                                            'RateCharge' => 127.50
                                            ),
                                    'Taxes' => array(
                                            'Tax2Amount' => 154.04,
                                            'Tax2Rate' => 0.13
                                            ),
                                    0 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                          ),
                                    1 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                            )
                                    ),
                            1 => array(
                                    'RateVendor' => 42074,
                                    'RateId' => 13,
                                    'RentalLocationID' => 262,
                                    'RateCompanyID' => 'RCR',
                                    'TotalPricing' => array(
                                            'RentalDays' => 1,
                                            'RateCharge' => 127.50
                                            ),
                                    'Taxes' => array(
                                            'Tax2Amount' => 154.04,
                                            'Tax2Rate' => 0.13
                                            ),
                                    0 => array(
                                            'DailyExtra' => array(
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )

                                            ),

                                    1 => array(
                                            'DailyExtra' => array (
                                                    'ExtraCode' => 'ERF',
                                                    'ExtraDesc' => 'ENERGY RECOVERY FEES'
                                                    )
                                            )
                                    )
                            )
                    )
            );

    array_to_xml($array, $doc, $pre);

    $doc->appendChild($pre);

echo $doc->saveXML() . "\n";