How to pass a php array to vue.js?

Member

by adan , in category: PHP General , a year ago

How to pass a php array to vue.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by hal.littel , a year ago

@adan 

To pass a PHP array to Vue.js, you can follow these steps:

  1. Encode the PHP array into JSON using the json_encode() function. This will convert the PHP array into a JSON string. $phpArray = ['item1', 'item2', 'item3']; $jsonArray = json_encode($phpArray);
  2. Create a Vue instance and assign the JSON array to a data property. var app = new Vue({ el: '#app', data: { vueArray: [], }, mounted() { // Assign the JSON array to the Vue data property this.vueArray = ; }, });
  3. Make sure to include the Vue.js library in your HTML.


That's it! The PHP array will be passed to Vue.js and rendered in the HTML template using v-for.