admin 管理员组

文章数量: 1087139


2024年4月23日发(作者:importance to)

通过

PHP

语言可以实现数据格式转换功能。以下是一些常见的数据格式转换示例:

转数组:

1$jsonString='{"name":"John","age":30,"city":"NewYork"}';

2$array=json_decode($jsonString,true);

2.

数组转

JSON

1$array=array("name"=>"John","age"=>30,"city"=>"NewYork");

2$jsonString=json_encode($array);

转数组:

1$xmlString='John30NewYork

>';

2$xml=simplexml_load_string($xmlString);

3$json=json_encode($xml);

4$array=json_decode($json,true);

4.

1

2

3

4

数组转

XML

$array=array("name"=>"John","age"=>30,"city"=>"NewYork");

$xml=newSimpleXMLElement('');

array_walk_recursive($array,array($xml,'addChild'));

$xmlString=$xml->asXML();

转数组:

1

2

3

4

5

6

$csvFile=fopen('','r');

$array=array();

while(($line=fgetcsv($csvFile))!==false){

$array[]=$line;

}

fclose($csvFile);

数组转

CSV

6.

1

2

3

4

5

6

7

8

9

10

$array=array(

array("John",30,"NewYork"),

array("Jane",25,"LosAngeles"),

array("Mike",35,"Chicago")

);

$csvFile=fopen('','w');

foreach($arrayas$line){

fputcsv($csvFile,$line);

}

fclose($csvFile);

1

以上是一些常见的数据格式转换示例,可以根据具体需求选择适合的方法进行数据格式转换。

2


本文标签: 数据格式 转换 方法 适合