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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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