# php-array-reader

> Reads PHP arrays from a string into a JavaScript object

Latest version **2.1.4** (published 2026-04-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install php-array-reader
pnpm add php-array-reader
yarn add php-array-reader
bun add php-array-reader
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 2.1.4 |
| Published | 2026-04-28 |
| First published | 2019-04-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 4 |
| Author | Bart van Raaij |
| Maintainers | bartvanraaij |
| Keywords | php, array |

## Links

- npm: https://www.npmjs.com/package/php-array-reader
- Repository: https://github.com/bartvanraaij/php-array-reader
- Homepage: https://github.com/bartvanraaij/php-array-reader#readme
- Issues: https://github.com/bartvanraaij/php-array-reader/issues
- npm.io page: https://npm.io/package/php-array-reader

## Dependencies (1)

- [php-parser](https://npm.io/package/php-parser.md) ^3.1.5

## Recent versions

- 2.1.4 (latest) — 2026-04-28
- 2.1.3 — 2026-01-07
- 2.1.2 — 2024-07-10
- 2.1.1 — 2023-10-23
- 2.1.0 — 2023-08-21
- 2.0.0 — 2023-08-17
- 1.3.5 — 2023-01-24
- 1.3.4 — 2022-04-12
- 1.3.2 — 2021-11-02
- 1.3.1 — 2021-11-02
- 1.3.0 — 2021-07-07
- 1.2.0 — 2019-04-18
- 1.1.1 — 2019-04-18
- 1.1.0 — 2019-04-18
- 1.0.0 — 2019-04-18

## README

# php-array-reader

[![Node.js CI](https://github.com/bartvanraaij/php-array-reader/actions/workflows/node.js.yml/badge.svg)](https://github.com/bartvanraaij/php-array-reader/actions/workflows/node.js.yml)

This small JS utility reads PHP strings containing arrays and returns a JavaScript object. 

It uses [glayzzle/php-parser](https://github.com/glayzzle/php-parser) to parse PHP into AST and uses that 
info to extract arrays.  
It supports both indexed and associative arrays (i.e. lists and dictionaries/maps) and array, string, numeric and null values.
Inline function calls are not evaluated but returned as raw strings. See the example below.

## Installation

This library is distributed with [npm](https://www.npmjs.com/package/php-array-reader) :

```sh
npm install php-array-reader --save
```

## Usage

```js
import { fromString } from 'php-array-reader';

const phpString = `[
  'key' => 'string',
  'list' => [
    'first',
    'second'
  ],
  'dictionary' => [
    'foo' => 'bar',
    'hello' => 'world'
  ],
  'also_supports' => null,
  'and_numeric' => 42,
  'what_about' => true,
  'or' => false,
  'func' => strtoupper('abc'),
]`;
const data = fromString(phpString); 
```
`data` will be this JS object:
```js
{
  key: 'string',
  list: ['first', 'second'],
  dictionary: {
    foo: 'bar',
    hello: 'world'
  },
  also_supports: null,
  and_numeric: 42,
  what_about: true,
  or: false,
  func: "strtoupper('abc')"
}
```

### With a PHP file

Use [`fs.readFileSync`](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) or another file reading library to read the file, and pass 
that string into `fromString`, e.g.:
```js
import { fromString } from 'php-array-reader';
import { readFileSync } from 'node:fs';

const phpFile = './file.php';
const phpString = readFileSync(phpFile);

const data = fromString(phpFile);
```

> [!NOTE]
> Version `1.x` of this library included a [`fromFile` method](https://github.com/bartvanraaij/php-array-reader/blob/a3f48acdef4eace2106ac40fa3c4593ab196dc1c/index.js#L6) 
> that allowed you to read a file directly. This has been removed in version `2.x` forward, because that method was a scope creep.

The PHP file can either return a single array, e.g.:
```php
<?php
return [
   'key' => 'string',
   'list' => [
     'first',
     'second'
   ],
   'dictionary' => [
     'foo' => 'bar',
     'hello' => 'world'
   ],
   'also_supports' => null,
   'and_numeric' => 42
];
```

This will have the same result as the `fromString` example above.

Or the PHP file may consist of multiple assigned arrays, e.g.:
```php
<?php
$first = [
    'key' => 'string',
    'dictionary' => [
        'foo' => 'bar',
        'hello' => 'world'
    ]
];
$second = [
    'list' => [
        'first','second'
    ],
    'also_supports' => null,
    'and_numeric' => 42  
];
```

This will return a JS object with the variable names as the first level keys:
```js
{
  first: {
    key: 'string',
    dictionary: {
      foo: 'bar', 
      hello: 'world'
    }
  },
  second: {
    list: ['first', 'second'],
    also_supports: null,
    and_numeric: 42
  }
}
```

---
_Source: https://npm.io/package/php-array-reader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
