Insert a converted php array in a JS Object

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
daniele8808
New php-forum User
New php-forum User
Posts: 1
Joined: Sun Dec 17, 2017 8:02 am

Sun Dec 17, 2017 8:12 am

I have a php array that i converted in Js using the json_encode method:

$mesi = array ( [0] => Nov [1] => Dic )

var js_mesi = '<?php print(json_encode($mesi)); ?>';

this is the converted array

["Nov","Dic"]

The problem I'm having now it's to place the Js array inside a JS object like this

data: {
labels: [js_mesi],
}

because the values of js_mesi are recognized as a single value and not as two separate values. The output I would like to have is this:

data: {
labels: ["Nov", "Dic"]
}

instead I see this

data: {
labels: ["Nov,Dic"]
}

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Sun Dec 17, 2017 4:21 pm

You'll need to provide more code; from what you have shown, you're mixing JavaScript and PHP.

Use the code blocks button </> when posting code.

Post Reply