yagmikita/array-utils

dev-master 2013-11-23 17:51 UTC

This package is not auto-updated.

Last update: 2024-09-24 05:10:48 UTC


README

  • Array2Xml - 类使用SimpleXMLElement类递归地将PHP关联数组转换为XML格式的字符串。
  • ArrayIterator - 类将PHP数组转换为可迭代的对象(比SPL库中的相同方法内存消耗更少)。

Array2Xml使用场景

  • 实例化转换器
 	<?php
        require_once realpath(__DIR__ . '/../vendor/autoload.php');
        use \ArrayUtils\Array2Xml;
  • 为了添加属性 - 添加键'@attributes',例如。
 		$a = [
 			'root' => [
 				'@attributes' => [
 					'attr1' => '1',
 				],
 			],
 		];
        $a2x = new Array2Xml($a);
        echo $a2x->conv();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root attr1="1"/>
  • 为了添加节点文本内容 - 添加键'@content',例如。
	<?php
		$a = [
		    'root' => [
		        'message' => [
		            '@content' => 'This is text content',
		        ],
		    ],
		];
        $a2x = new Array2Xml($a);
        echo $a2x->conv();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <message>This is text content</message>
</root>
  • 短 '@content' 语法,例如。
 	<?php
		$a = [
		    'root' => [
		        'person' => [
		            'gender' => 'male',
		            'age' => '26',
		            'name' => 'Александр',
		        ],
		    ],
		];
        $a2x = new Array2Xml($a);
        echo $a2x->conv();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <person>
    <gender>male</gender>
    <age>26</age>
    <name>Александр</name>
  </person>
</root>
  • 为了添加具有相同名称的标签,添加一个具有所需键名的枚举元素的数组,例如。
 	<?php
		$a = [
		    'root' => [
		        '@attributes' => [
		            'type' => 'test_type',
		            'id' => 'test_id',
		            'text' => 'русский текст'
		        ],
		        'main' => [
		            '@attributes' => [
		                'uid' => 'u201j3a0828ki3si0',
		                'src' => 'open',
		                'start' => '12423511247',
		                'stop' => '12423991978',
		                'adtp' => 'dbt',
		                'clid' => '100500',
		            ],
		            'attr' => [
		                [
		                    '@attributes' => [
		                        'code' => '1',
		                        'val' => '11',
		                    ],
		                ],
		                [
		                    '@attributes' => [
		                        'code' => '2',
		                        'val' => '12',
		                    ],
		                ],
		            ],
		            'history' => [
		                [
		                    '@attributes' => [
		                        'at1' => '1',
		                        'at2' => '2',
		                        'at3' => '3',
		                        'at4' => 'test',
		                    ],
		                ],
		            ],
		            'person' => [
		                'gender' => [
		                    	'@content' => 'male'
		                ],
		                'age' => [
		                    	'@content' => 26,
		                ],
		                'name' => [
		                    	'@content' => 'Александр'
		                ],
		            ],
		        ],
		    ],
		];
        $a2x = new Array2Xml($a);
        echo $a2x->conv();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root type="test_type" id="test_id" text="русский текст">
  <main uid="u201j3a0828ki3si0" src="open" start="12423511247" stop="12423991978" adtp="dbt" clid="100500">
    <attr code="1" val="11"/>
    <attr code="2" val="12"/>
    <history at1="1" at2="2" at3="3" at4="test"/>
    <person>
      <gender>male</gender>
      <age>26</age>
      <name>Александр</name>
    </person>
  </main>
</root>