PHPackages                             imran/data-converter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. imran/data-converter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imran/data-converter
====================

A PHP class that converts different data formats to each other.

00PHP

Since Feb 22Pushed 3y ago1 watchersCompare

[ Source](https://github.com/grim-reapper/data-converter)[ Packagist](https://packagist.org/packages/imran/data-converter)[ RSS](/packages/imran-data-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

DataConverter Class
===================

[](#dataconverter-class)

The DataConverter class provides a simple and convenient way to convert data from one format to another. Whether you need to convert a CSV file to JSON, or an array to YAML, this class has you covered. The supported formats include CSV, JSON, XML, YAML, serialized PHP data, and PHP objects.

This class has a user-friendly interface that allows you to easily convert data in either a string or file format. Additionally, it has several methods that allow you to fine-tune the conversion process, such as specifying custom root elements and row elements for XML conversions.

With its comprehensive set of conversion methods, the DataConverter class is a must-have tool for any PHP developer. Whether you're working on a small personal project or a large enterprise application, this class can help you simplify your data conversion needs.

Features
--------

[](#features)

- Converts CSV to Array
- Converts Array to CSV
- Converts CSV to JSON
- Converts JSON to CSV
- Converts JSON to Array
- Converts Array to JSON
- Converts XML to Array
- Converts Array to XML
- Converts XML to JSON
- Converts JSON to XML
- Converts Array to Serialize
- Converts Serialize to Array
- Converts Yaml to Array
- Converts Array to Yaml
- Convert CSV to XML
- Converts XML To CSV

Requirements
------------

[](#requirements)

- PHP 8.0 or Higher
- PHP XML Extension: `ext-simplexml`
- PHP Yaml Extension: `ext-yaml`

Installation
------------

[](#installation)

First, include the DataConverter.php class in your project.

```
require_once 'path/to/DataConverter.php';
```

or by using composer `Recommended way`

```
composer require imran/data-converter
```

Usage
-----

[](#usage)

Then, create an instance of the DataConverter class:

```
use Imran\DataConverer\DataConverer;

$dataConverter = new DataConverter();
```

Methods
-------

[](#methods)

### CSV to Array

[](#csv-to-array)

```
$csvData = "name,email,phone
John Doe,johndoe@example.com,1234567890
Jane Doe,janedoe@example.com,0987654321";

$arrayData = $dataConverter->csvToArray($csvData);
print_r($arrayData);
```

##### OUTPUT

[](#output)

```
Array
(
    [0] => Array
        (
            [name] => John Doe
            [email] => johndoe@example.com
            [phone] => 1234567890
        )

    [1] => Array
        (
            [name] => Jane Doe
            [email] => janedoe@example.com
            [phone] => 0987654321
        )
)
```

### Array to CSV

[](#array-to-csv)

```
$arrayData = [
    [
        'name' => 'John Doe',
        'email' => 'johndoe@example.com',
        'phone' => '1234567890'
    ],
    [
        'name' => 'Jane Doe',
        'email' => 'janedoe@example.com',
        'phone' => '0987654321'
    ]
];

$csvData = $dataConverter->arrayToCsv($arrayData);
print_r($csvData);
```

##### OUTPUT

[](#output-1)

```
name,email,phone
John Doe,johndoe@example.com,1234567890
Jane Doe,janedoe@example.com,0987654321
```

### JSON to Array

[](#json-to-array)

```
$jsonData = '[{"name":"John Doe","email":"johndoe@example.com","phone":"1234567890"},{"name":"Jane Doe","email":"janedoe@example.com","phone":"0987654321"}]';

$arrayData = $dataConverter->jsonToArray($jsonData);
print_r($arrayData);
```

##### OUTPUT

[](#output-2)

```
Array
(
    [0] => Array
        (
            [name] => John Doe
            [email] => johndoe@example.com
            [phone] => 1234567890
        )

    [1] => Array
        (
            [name] => Jane Doe
            [email] => janedoe@example.com
            [phone] => 0987654321
        )
)
```

### Array to JSON

[](#array-to-json)

```
$arrayData = [
    [
        'name' => 'John Doe',
        'email' => 'johndoe@example.com',
        'phone' => '1234567890'
    ],
    [
        'name' => 'Jane Doe',
        'email' => 'janedoe@example.com',
        'phone' => '0987654321'
    ]
];

$jsonData = $dataConverter->arrayToJson($arrayData);
print_r($jsonData);
```

##### OUTPUT

[](#output-3)

```
[{"name":"John Doe","email":"johndoe@example.com","phone":"1234567890"},{"name":"Jane Doe","email":"janedoe@example.com","phone":"0987654321"}]
```

### XML to Array

[](#xml-to-array)

```
$xmlData = '

        John Doe
        johndoe@example.com
        1234567890

        Jane Doe
        janedoe@example.com
        0987654321

';

$arrayData = $dataConverter->xmlToArray($xmlData);
print_r($arrayData);
```

##### OUTPUT

[](#output-4)

```
Array
(
    [items] => Array
    (
        [0] => Array
        (
            [name] => John Doe
            [email] => johndoe@example.com
            [phone] => 1234567890
        )

        [1] => Array
        (
            [name] => Jane Doe
            [email] => janedoe@example.com
            [phone] => 0987654321
        )
    )
)
```

### Array to XML

[](#array-to-xml)

```
$arrayData = [
    [
        'name' => 'John Doe',
        'email' => 'johndoe@example.com',
        'phone' => '1234567890'
    ],
    [
        'name' => 'Jane Doe',
        'email' => 'janedoe@example.com',
        'phone' => '0987654321'
    ]
];

$xmlData = $dataConverter->arrayToXml($arrayData);
print_r($xmlData);
```

##### OUTPUT

[](#output-5)

```

    John Doe
    johndoe@example.com
    1234567890

    Jane Doe
    janedoe@example.com
    0987654321

```

### unserialize

[](#unserialize)

```
$dataConverter = new DataConverter();
$serializedData = "a:3:{s:4:\"name\";s:8:\"John Doe\";s:5:\"email\";s:17:\"johndoe@example.com\";s:5:\"phone\";s:6:\"123456\";}";
$unserializedData = $dataConverter->unserialize($serializedData, 'serialized');
print_r($unserializedData);
```

##### OUTPUT

[](#output-6)

```
Array
(
    [name] => John Doe
    [email] => johndoe@example.com
    [phone] => 123456
)
```

### serialize

[](#serialize)

```
$dataConverter = new DataConverter();
$arrayData = array(
    'name' => 'John Doe',
    'email' => 'johndoe@example.com',
    'phone' => '123456'
);
$serializedData = $dataConverter->serialize($arrayData, 'serialized');
echo $serializedData;
```

##### OUTPUT

[](#output-7)

```
a:3:{s:4:"name";s:8:"John Doe";s:5:"email";s:17:"johndoe@example.com";s:5:"phone";s:6:"123456";}
```

### yamlToArray

[](#yamltoarray)

```
$dataConverter = new DataConverter();
$yamlData = "name: John Doe
email: johndoe@example.com
phone: 123456";
$arrayData = $dataConverter->yamlToArray($yamlData);
print_r($arrayData);
```

##### OUTPUT

[](#output-8)

```
Array
(
    [name] => John Doe
    [email] => johndoe@example.com
    [phone] => 123456
)
```

### arrayToYaml

[](#arraytoyaml)

```
$dataConverter = new DataConverter();
$arrayData = array(
    'name' => 'John Doe',
    'email' => 'johndoe@example.com',
    'phone' => '123456'
);
$yamlData = $dataConverter->arrayToYaml($arrayData);
echo $yamlData;
```

##### OUTPUT

[](#output-9)

```
name: John Doe
email: johndoe@example.com
```

### csvToJson

[](#csvtojson)

```
$csv = "name,age,city\nJohn,30,New York\nJane,25,London";

$json = $dataConverter->csvToJson($csv);

// Output:
// '[{"name":"John","age":"30","city":"New York"},{"name":"Jane","age":"25","city":"London"}]'
```

### jsonToCsv

[](#jsontocsv)

```
$json = '[{"name":"John","age":"30","city":"New York"},{"name":"Jane","age":"25","city":"London"}]';

$csv = $dataConverter->jsonToCsv($json);

// Output:
// "name,age,city\nJohn,30,New York\nJane,25,London"
```

### xmlToJson

[](#xmltojson)

```
$xml = '

    John
    30
    New York

    Jane
    25
    London

';

$json = $dataConverter->xmlToJson($xml);

// Output:
// '[{"name":"John","age":"30","city":"New York"},{"name":"Jane","age":"25","city":"London"}]'
```

### jsonToXml

[](#jsontoxml)

The method takes two arguments: the JSON string and an optional root element name (defaults to "root"). The method returns the resulting XML string.

```
$json = '[{"name":"John","age":"30","city":"New York"},{"name":"Jane","age":"25","city":"London"}]';

$xml = $dataConverter->jsonToXml($json);

// Output:
// '
//
//     John
//     30
//     New York
//
//
//     Jane
//     25
//     London
//
// '
```

### csvToXml

[](#csvtoxml)

The csvToXml method converts a CSV string to an XML string.

#### Usage

[](#usage-1)

The method takes three arguments: the CSV string, an optional delimiter

```
$dataConverter = new DataConverter();
$csv =

    1
    John Doe
    32

    2
    Jane Doe
    29

```

You can customize the root element name and row element name by passing the optional second and third arguments to the method. For example:

```
$xml = $dataConverter->csvToXml($csv, 'persons', 'person');
echo $xml;
```

This will produce the following XML output:

```

    1
    John Doe
    32

    2
    Jane Doe
    29

```

### Running Tests

[](#running-tests)

To run tests, use following command

```
.\vendor\bin\phpunit tests/DataConverterTest.php
```

### Hi, I'm Imran Ali! 👋

[](#hi-im-imran-ali-)

### 🚀 About Me

[](#-about-me)

Senior **Full-Stack** Developer specializing in front end and back-end development. Experienced with all stages of the development cycle for dynamic web projects. Innovative, creative and a proven team player, I possess a Tech Degree in Front End Development and have 7 years building developing and managing websites, applications and programs for various companies. I seek to secure the position of Senior Full Stack Developer where i can share my skills, expertise and experience with valuable clients.

### 🛠 Skills

[](#-skills)

PHP OOP, Laravel, Codeigniter Javascript, Node, React, Vue, Git, HTML, Rest Api, Typescript, Angular, SCSS, Docker, CI/CD Jenkins, Bootstrap, Responsive Design, ASP.NET Core

### 🔗 Follow on

[](#-follow-on)

[![linkedin](https://camo.githubusercontent.com/9745a59ad4a919d2e524512c3f732c820d4aa0fe927def4f37c4aba08f7e42bc/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b6564696e2d3041363643323f7374796c653d666f722d7468652d6261646765266c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465)](https://www.linkedin.com/in/imranali291/)[![twitter](https://camo.githubusercontent.com/79cbea7e1aa9ec614d34050bdc4128542d8527807ac8ce879f953ced3478375d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d3144413146323f7374796c653d666f722d7468652d6261646765266c6f676f3d74776974746572266c6f676f436f6c6f723d7768697465)](https://twitter.com/imranali125)

### License

[](#license)

[![MIT License](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://choosealicense.com/licenses/mit/)[![GPLv3 License](https://camo.githubusercontent.com/da9c3abfd62c32a94031c3a382cb7c85dbd4cede411416837adfe6b8fda05ba1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d47504c25323076332d79656c6c6f772e737667)](https://opensource.org/licenses/)[![AGPL License](https://camo.githubusercontent.com/aed477ac82de60abd644cfa7c9d381eebe9a00d5d32168644f7e6d2a23957d1b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4147504c2d626c75652e737667)](http://www.gnu.org/licenses/agpl-3.0)

### Contributing

[](#contributing)

Contributions are always welcome!

See `contributing.md` for ways to get started.

Please adhere to this project's `code of conduct`.

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/7252105628ffa6f064585370161626c7c72e68dd2e74ee66aa50d5894543c183?d=identicon)[webz2feel](/maintainers/webz2feel)

---

Top Contributors

[![grim-reapper](https://avatars.githubusercontent.com/u/7957389?v=4)](https://github.com/grim-reapper "grim-reapper (3 commits)")

### Embed Badge

![Health badge](/badges/imran-data-converter/health.svg)

```
[![Health](https://phpackages.com/badges/imran-data-converter/health.svg)](https://phpackages.com/packages/imran-data-converter)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
