DOM with Arrays
Learn how to create a new Inphinit project.
DOM to Array
{placeholder}
use Inphinit\DOM\Document;
$handle = new Document(Document::XML);
$handle->load('<root xmlns:book="https://book-io"><node foo="bar" baz="foobar">contents</node><book:tag>baz</book:tag></root>');
echo "COMPLETE:\n";
print_r($handle->toArray(Document::ARRAY_COMPLETE));
echo "SIMPLE:\n";
print_r($handle->toArray(Document::ARRAY_SIMPLE));
echo "MINIMAL:\n";
print_r($handle->toArray(Document::ARRAY_MINIMAL));
{placeholder}
print_r($handle->toArray(Document::ARRAY_COMPLETE));
output:
Array
(
[root] => Array
(
[node] => Array
(
[@attributes] => Array
(
[foo] => bar
[baz] => foobar
)
[@contents] => contents
)
[book:tag] => baz
[@attributes] => Array
(
[xmlns:book] => https://book.io
)
)
)
{placeholder}
print_r($handle->toArray(Document::ARRAY_SIMPLE));
output:
Array
(
[root] => Array
(
[node] => Array
(
[@attributes] => Array
(
[foo] => bar
[baz] => foobar
)
[@contents] => contents
)
[book:tag] => baz
[@attributes] => Array
(
[xmlns:book] => https://book.io
)
)
)
{placeholder}
print_r($handle->toArray(Document::ARRAY_MINIMAL));
output:
Array
(
[root] => Array
(
[node] => contents
)
)
Array to HTML
{placeholder}
use Inphinit\DOM\Document;
$handle = new Document(Document::HTML);
$handle->fromArray([
'html' => [
'head' => [
'@contents' => 'contents <title>test</title>',
],
'body' => [
'main' => [
'p' => [
'@contents' => 'contents <s>test</s>',
'span' => [
'foo',
'bar',
'baz'
]
],
'div' => [
'foo',
'bar',
'baz'
],
'@comment' => 'test'
],
'@attributes' => [
'data-foo' => 'bar',
'data-baz' => 'foobar'
]
],
'@attributes' => [
'class' => 'sample',
'id' => 'test'
]
]
]);
$handle->dump($handle->root());
Array to HTML
{placeholder}
use Inphinit\DOM\Document;
$handle = new Document(Document::XML);
$handle->fromArray([
'root' => [
'node' => [
'@attributes' => [
'foo' => 'bar',
'baz' => 'foobar'
],
'foo' => [
'bar',
'baz'
],
'@contents' => 'contents <s>test</s>',
],
'book:tag' => 'baz',
'@attributes' => [
'class' => 'sample',
'xmlns:book' => 'https://book.io'
],
'@comment' => 'foobar'
]
]);
$handle->dump($handle->root());