0

Does anyone have a smart idea how to transform this javascript object into an associative php array?

Note that I don't want to read this into PHP, I want the same data structure in PHP without going through each line and editing it e.g. adding '' and =>.

module.exports = {
  base: '/',
  api: {
    sentry: 'https://[email protected]/257382',
    wp: {
      imgSize: {
        _380x270: 'medium',
      }
    }
  },
  y: 2017,
  href: {
    authors: '#',
    fb: '#',
    instagram: '#',
    de: 'https://wearede.com/',
  },
  faction: {
    subscribe: "/",
    search: "/",
  },
  search: {
    tags: [{
        t: 'Lifestyle',
        h: '#'
      },
      {
        t: 'Politics',
        h: '#'
      },
      {
        t: 'Economy',
        h: '#'
      },
    ],
    articles: [{
        t: '�??�?��?��??�?��?��?? �?��??�?��??�?��?��?? �?��??�?��?? �?��??�?��??�?��??�?� �?��?? �?��?��??�?� �?��??�?��??�?��??�?� �?��?��?��?��?��?��??�?��?? �?��?��?��?� �?��??�?��?��??�?��?��?��?��?��??',
        h: '#',
      },
      {
        t: '�?��??�?��?? �?��?��?��?��?��?��??�?� �?��??�?��??�?��?��?��?��??�?� �?��?��?��?��?��?��?��?��??�?� �?��??�?��?��??�?��?��?��?��??',
        h: '#',
      },
    ]
  },
  menuMain: [{
      h: '#',
      t: '�?��??�??�?��?��?��?��?��??'
    },
    {
      h: '#',
      t: '�?��??�?��?��?��?��?? �?��?? �?��?��?��?��?��?��?��?��??�??'
    },
    {
      h: '#',
      t: 'Lifestyle'
    },
    {
      h: '#',
      t: '�?��?��?��?��??�?��?��?��?��??'
    },
    {
      h: '#',
      t: '�?��??�?��?��?��?��?��?��?��??�??'
    },
    {
      h: '#',
      t: '<i class="i i--hammock fz-20"></i>'
    },
  ],
  footer: {
    authors: [{
        i: 'https://picsum.photos/55/55',
        h: '#',
        n: 'Rusudan Tinatin',
        t: 'Author',
        d: '�?��??�?��?? �?��?��?��?��?��?��??�?� �?��??�?��??�?��?��?��?��??�?� �?��?��?��?��?��?��?��?��??�?� �?��??�?��?��??�?��?��?��?��??',
      },
      {
        i: 'https://picsum.photos/55/55',
        h: '#',
        n: 'Rusudan Tinatin',
        t: 'Author',
        d: '�??�?��?��??�?��?��?? �?��??�?��??�?��?��?? �?��??�?��?? �?��??�?��??�?��??�?� �?��?? �?��?��??�?� �?��??�?��??�?��??�?� �?��?��?��?��?��?��??�?��?? �?��?��?��?� �?��??�?��?��??�?��?��?��?��?��??',
      },
      {
        i: 'https://picsum.photos/55/55',
        h: '#',
        n: 'Rusudan Tinatin',
        t: 'Author',
        d: '�?��??�?��?? �?��?��?��?��?��?��??�?� �?��??�?��??�?��?��?��?��??�?� �?��?��?��?��?��?��?��?��??�?� �?��??�?��?��??�?��?��?��?��??',
      },
    ],
    nav: [{
        t: '�?��??�?��?��?��?��?��??',
        h: '#'
      },
      {
        t: '�?��??�?��??�?��?? �??�?��?��?��?��?��??�?��??�??',
        h: '#'
      },
      {
        t: '�?��?��?��?��?� �?��?��?��??�?��?��?�',
        h: '#'
      },
      {
        t: '�?��?��?��?��??�?��?��??',
        h: '#'
      },
      {
        t: '�??�?��?��?��?��?��?��??',
        h: '#'
      },
    ],
    cats: [{
        t: '�?��??�??�?��?��?��?��?��??',
        h: '#'
      },
      {
        t: 'Lifestyle',
        h: '#'
      },
      {
        t: '�?��??�?��?��?��?��?��?��?��??�??',
        h: '#'
      },
      {
        t: '�?��??�?��?��?��?��?? �?��?? �?��?��?��?��?��?��?��?��??�??',
        h: '#'
      },
      {
        t: '�?��?��?��?��??�?��?��?��?��??',
        h: '#'
      },
      {
        t: 'Hammock',
        h: '#'
      },
    ],
    articles: {
      items: articles.slice(0, 3),
      i: 'https://picsum.photos/380/260',
      t: '�?��?��?��??�?��?��?��?? �?��??�?��?��?��?? �?��??�?��??�?��??�?��?� �?��??�?��??�?��??�?��?��?��?��?� �?��??�?��?��??�?��??�?��?��?� �?��?��?�',
      h: '#',
    },
  }
}

At the moment I'm considering an option to record macros in vim, but that will take quite some time, and I see potential pitfalls in that approach.

5
  • 4
    Use JSON.stringify() in Javascript, parse it in PHP with json_decode(), then use var_export() to print it as a PHP array literal. Commented Dec 15, 2017 at 17:56
  • BTW, that's not an array of Javascript objects. It's a single object. Some of the properties contain arrays of objects. Commented Dec 15, 2017 at 17:58
  • @Barmar I was missing the knowledge of var_export() thanks! also amended question, your right. Commented Dec 15, 2017 at 18:00
  • @LorenzMeyer I'm fine with you answering. We both thought of it at the same time. Commented Dec 15, 2017 at 18:02
  • @Barmar since your both fine, I'm accepting the answer. Commented Dec 16, 2017 at 8:07

1 Answer 1

3

Convert this object into a JSON string. Then in PHP, you can json_decode and var_export.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.