PHP code example of selfphp / data-converter
1. Go to this page and download the library: Download selfphp/data-converter 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/ */
selfphp / data-converter example snippets
use Selfphp\DataConverter\Format\ArrayToXmlConverter;
$array = [
'user' => [
'name' => 'Alice',
'active' => true,
'note' => null
]
];
$xml = ArrayToXmlConverter::convertArray(
$array,
rootElement: 'response',
addXmlDeclaration: true,
convertNullToXsiNil: true,
convertBoolToString: true
);
echo $xml;
use Selfphp\DataConverter\Format\XmlToArrayConverter;
$xml = <<<XML
<user id="42" active="true">Alice</user>
XML;
$converter = new XmlToArrayConverter();
$array = $converter->convert($xml);
print_r($array);
// [
// '@id' => '42',
// '@active' => 'true',
// '#text' => 'Alice'
// ]
use Selfphp\DataConverter\Format\ArrayToJsonConverter;
$data = ['url' => 'https://example.com'];
$converter = (new ArrayToJsonConverter())
->withFlags(JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
echo $converter->convert($data);
use Selfphp\DataConverter\Format\JsonToArrayConverter;
$json = '{"name":"Alice","active":true}';
$converter = new JsonToArrayConverter();
$array = $converter->convert($json);
['b' => 'bold'] // Text parts around <b> are not preserved
['active' => 'true'] // not boolean true
src/
└── Format/
├── ArrayToXmlConverter.php
├── XmlToArrayConverter.php
├── ArrayToJsonConverter.php
└── JsonToArrayConverter.php
tests/
└── Format/
├── ArrayToXmlConverterTest.php
├── XmlToArrayConverterTest.php
├── ArrayToJsonConverterTest.php
└── JsonToArrayConverterTest.php