you have already done it: var data2 = ; just remove the brackets [ ]

also assign key to your array e.g. array('key'=>'value')

$array = array(label=>value, name=>value);

var data2 = <?php echo json_encode($dataset2 );?>;
Answer from Fury on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-PHP-array-to-JavaScript-array
How to convert PHP array to JavaScript array?
June 18, 2020 - You can use PHP array in JavaScript. It works for the single as well as the multidimensional array. Use the json_encode() method to achieve this. ... Converting PHP array into JavaScript.
🌐
Laracasts
laracasts.com › discuss › channels › laravel › how-to-pass-entire-array-from-controller-to-view-in-js
How to pass entire array from controller to view in js?
Well when you pass an array to json_encode it converts 0 indexed arrays to javascript arrays and associative arrays to javascript objects
🌐
CodexWorld
codexworld.com › home › how to guides › how to convert php array to javascript array
How to Convert PHP Array to JavaScript Array - CodexWorld
February 26, 2025 - Using json_encode() function, PHP array can be converted to JavaScript array and accessible in JavaScript.
🌐
Laracasts
laracasts.com › discuss › channels › general-discussion › convert-php-array-to-javascript-array-for-jquery-autocomplete
Convert php array to javascript array for jquery autocomplete
in controller file : public function psearchdisplay() { $users = DB::table('people1')->get(); $result_array = array(); foreach ($users as $p => $val ) { $result_array[] = array($val->surname); } $usersjson = json_encode($result); return ...
🌐
DbeTech
dbestech.com › tutorials › passing-laravel-array-in-javascript
Passing (laravel) Array in Javascript
2. You can use laravel json directive · var app = @json($yourarray); It's very convenient · 3. Curly braces and exclamation marks · var array = {!! json_encode($yourarray) !!}; 4. Json parse and encode together · var array = JSON.parse('{!! json_encode($yourarray) !!}'); 5. With PHP tag ·
🌐
DEV Community
dev.to › ankitvermaonline › convert-php-array-to-javascript-2050
Convert php array to Javascript - DEV Community
May 27, 2025 - <script type="text/javascript"> // Assign PHP user array to a JS variable var user = <?php echo json_encode($user); ?>; console.log(user); </script> ... Full Stack Developer 7+ years of experience in Web development, specializing in PHP frameworks. ... #laravel #php #backup #devops Download and Save WhatsApp Media Using Media ID (WhatsApp Cloud API) in Laravel and Core PHP
Find elsewhere
🌐
CreativeDev
thecreativedev.com › home › how to convert php array to javascript array?
How to convert PHP array to JavaScript array? | CreativeDev
September 15, 2015 - //PHP $new_arr = array(); foreach($exist_array as $key =>$value) { $new_arr.='"'.$key.'":"'.$value.'",'; } $new_arr=substr($exist_array,0,-1); echo $new_arr="{".$new_arr."}"; Here, I have used foreach loop and convert into JavaScript json encode ...
🌐
Stack Overflow
stackoverflow.com › questions › 31829371 › convert-laravel-object-to-js-array
php - Convert laravel object to js array - Stack Overflow
May 23, 2017 - I am new in laravel. How to convert laravel object into js array. my coce is below.. $customer = new Customer(); $result = $customer->where('client_state','like', '%'.$statename.'%...
🌐
W3docs
w3docs.com › home › code snippets › php › how to convert a php array to a javascript array
How to Convert a PHP Array to a JavaScript Array | W3docs
To convert a PHP array to a JavaScript array, you should pass it and then apply <kbd class="highlighted">json_encode</kbd>.
🌐
Tech Blog
tech.auct.eu › convert-php-array-to-javascript-array
Convert php array to javascript array
March 28, 2023 - To convert php array to javascript array use json_encode() Simple plain array ; const b = ; Associative array ; const b = ; Happy coding!
🌐
Medium
sergheipogor.medium.com › how-to-make-php-arrays-into-javascript-arrays-5d9108f4ea85
How to Make PHP Arrays into JavaScript Arrays | by Serghei Pogor | Medium
March 30, 2024 - One of the simplest ways to convert PHP arrays to JavaScript arrays is by using JSON encoding and decoding. JSON (JavaScript Object Notation) is a lightweight data interchange format that’s widely supported in both PHP and JavaScript.
🌐
Stack Overflow
stackoverflow.com › questions › 49269963 › how-to-convert-php-array-to-javascript-array-in-php
How to convert php array to javascript array in php - Stack Overflow
I'm working on core php,i need how to convert PHP array to Javascript array please help me,below the my example code this there please check it. I tried from long time to debug but not getting any...
🌐
YouTube
youtube.com › watch
How to convert a php array to javascript array - YouTube
In this video we'll discuss that how to pass or convert a php array to javascript.
Published: July 25, 2020
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-pass-a-php-array-to-a-javascript-function
How to pass a PHP array to a JavaScript function? - GeeksforGeeks
July 11, 2025 - In this approach, we encode the PHP array as JSON and embed it into a hidden input element in the HTML. Then, in the JavaScript section, we retrieve the value of the hidden input and parse it back to a JavaScript array using JSON.parse().