vacek Posted March 31, 2014 Share Posted March 31, 2014 Hi, For whatever reason (stupid) I cannot get my head wrapped around this. I have 6 PHP arrays (data I grab from MySQL that is dynamic so might be one result, might be 60) that I need to assign to a javascript array so I can create graphs with the data. PHP arrays are as follows: $billable, $core, $available, $oto, $training, $name The Javascript array needs to look like the following: var items = [{ "Billable": 20, "Core": 20, "Available": 30, "OTO": 20, "Training": 10, "Name": "Bob" },{ "Billable": 20, "Core": 30, "Available": 20, "OTO": 0, "Training": 30, "Name": "John" },{ "Billable": 10, "Core": 80, "Available": 10, "OTO": 0, "Training": 30, "Name": "Ralph" }]; I've been fiddling with this for some time, but cannot get it to work. Any thoughts of a quick way to cycle through and build this javascript array ? Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/ Share on other sites More sharing options...
Ch0cu3r Posted March 31, 2014 Share Posted March 31, 2014 he Javascript array needs to look like the following: That is json. PHP has a built-in function for converting an array to JSON for you called json_encode Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/#findComment-1474502 Share on other sites More sharing options...
vacek Posted March 31, 2014 Author Share Posted March 31, 2014 That is json. PHP has a built-in function for converting an array to JSON for you called json_encode Been fiddling with this, but cannot seem to get any further ahead. Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/#findComment-1474505 Share on other sites More sharing options...
Ch0cu3r Posted March 31, 2014 Share Posted March 31, 2014 What is your current code? Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/#findComment-1474507 Share on other sites More sharing options...
vacek Posted March 31, 2014 Author Share Posted March 31, 2014 What is your current code? I'm starting over from scratch ... nothing I tried has worked. Once I get something worked back up, I'll be happy to paste it here. Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/#findComment-1474508 Share on other sites More sharing options...
kicken Posted March 31, 2014 Share Posted March 31, 2014 <script type="text/javascript"> var items = <?php echo json_encode($items); ?>; </script> Quote Link to comment https://forums.phpfreaks.com/topic/287412-assign-php-array-to-javascript-array/#findComment-1474583 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.