PHP code example of shanyuliang / array2xml

1. Go to this page and download the library: Download shanyuliang/array2xml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shanyuliang / array2xml example snippets


use Shanyuliang\Array2xml\Array2xml;

$arr = [
    'aaa' => [
        'bbb' => 'ccc'
    ]
];
$array2xml = new Array2xml();

$result = $array2xml->generate($arr);

{
    'code'  => 0,
    'errer' => '',
    'data'  => '<?xml version="1.0" encoding="utf-8"

use Shanyuliang\Array2xml\Array2xml;

$arr = [
    'aaa' => [
        'bbb' => 'ccc',
        'eee' => 'fff'
    ]
];

//add charset and version, default charset: utf-8 version 1.0
$array2xml = new Array2xml('utf-8', '1.0');

//add attribute for key
$attribute = [
    'aaa' => [
        'xmlns' => 'https://www.shanyuliang.com',
        'date'  => '2019-8-31'
    ]
];

//add cdata for key
$cdata = ['bbb'];

$result = $array2xml->generate($arr, $attribute, $cdata);

{
    'code'  => 0,
    'errer' => '',
    'data'  => '<?xml version="1.0" encoding="utf-8"

use Shanyuliang\Array2xml\Array2xml;

$arr = [
    'xmlHead'   => [
        'A' => 'aaa',
        'B' => 'bbb',
        'C' => [
            'Aa' => '111',
            'Ab' => '222'
        ],
        'D|1'   => [
            'D1'    => 'd1'
        ],
        'D|2'   => [
            'D2'    => 'd2'
        ]
    ]
];

$array2xml = new Array2xml();

$result = $array2xml->generate($arr);

{
    'code'  => 0,
    'errer' => '',
    'data'  => '<?xml version="1.0" encoding="utf-8"

public function getXml(Array2xml $array2xml) 
{
    $arr = [
        'aaa' => [
            'bbb' => 'ccc',
            'eee' => 'fff'
        ]
    ];
    $response = $array2xml->generate($arr);
}

public function getXml() 
{
    $arr = [
        'aaa' => [
            'bbb' => 'ccc',
            'eee' => 'fff'
        ]
    ];
    $response = app('array2xml')->generate($arr);
}