npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-php-data

v2.0.0

Published

Convert simple JS data to PHP

Downloads

7,783

Readme

JS data to PHP converter

Test status in GitHub Actions Version on npm

This package takes a JavaScript expression and converts it into a PHP expression.

✨ Try it out in the online demo.

Why?

For a PHP-oriented full stack developer, it's quite common having to convert JavaScript (or even JSON) data into a PHP array. This is tedious to do manually and I wanted a copy&paste solution.

Therefore, the actual purpose of this package is its online demo, the package is merely a byproduct.

Installation

Install it from npm:

npm install js-php-data

Use in Node.js

// In CommonJS modules
const jsPhpData = require('js-php-data')

// In module packages
import jsPhpData from 'js-php-data'

Use in the Browser

You can use this package in your browser. It is compiled to ES5, so it runs in all major browsers down to IE 11.

This build exposes a global jsPhpData function and relies on the prettier and prettierPlugins.php globals to already be loaded:

<script src="https://unpkg.com/js-php-data/dist/js-php-data.umd.js"></script>

Usage

Once you have somehow imported the jsPhpData function from this module, you can use it by passing it a value.

jsPhpData({
  foo: 'bar',
  baz: 5
})

/*
[
    'foo' => 'bar',
    'baz' => 5
]
*/

You may also pass some optional configuration options:


castToObject

Type: boolean

Default: false

PHP arrays are used for converted JavaScript arrays as well as JavaScript objects.

This is usually fine, but you can enable this option to make the distinction extra clear by prepending an (object) type cast to converted objects. This is especially useful if you want to convert the result back to JavaScript later (for example with json_encode), since sometimes converted arrays and objects are then indistinguishable.


bracketArrays

Type: boolean

Default: true

Use PHP's bracket [] notation for arrays. If set to false, the older/more compatible array() notation will be used.


trailingCommas

Type: boolean

Default: false

If set to true, the last items of all arrays will have a comma appended.


indentation

Type: number | "tab"

Default: 2

By how many spaces arrays should be indented. If set to "tab", one tab will be used instead.


quotes

Type: "single" | "double"

Default: "single"

Which quotes to use to delimit creating strings.


removeUndefinedProperties

Type: boolean

Default: true

If set to true, properties with an undefined value will be omitted from the output.

If set to false, the value will be replaced with null.


onCircular

Type: "null" | "nullWithComment" | "string" | "throw"

Default: "nullWithComment"

How to handle circular references.

  • "null": replace them with null
  • "nullWithComment": replace them with null and a adjoining /* CIRCULAR */ comment
  • "string": replace them with "::CIRCULAR::"
  • "throw": Throw an error

Known limitations

This tool is about converting plain structures. Resolving circular dependencies or converting objects with prototypes is not supported on purpose.