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 Overflowyou 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 );?>;
As an alternative to the other answers - you could consider using a package for this. Jeffrey Way from Laracasts made this package laracasts/PHP-Vars-To-Js-Transformer
JavaScript::put('data2', $dataset2);
I'm going to assume that the two arrays you've given for PHP and JS are not related, and they're just examples of how arrays look in the two languages. Clearly you're not going to be able to convert those sequences of letters and numbers into those city names.
PHP provides a function to convert PHP arrays into Javascript code: json_encode(). (technically, it's JSON format; JSON stands for JavaScript Object Notation)
Use it like this:
<script type='text/javascript'>
<?php
$php_array = array('abc','def','ghi');
$js_array = json_encode($php_array);
echo "var javascript_array = ". $js_array . ";\n";
?>
</script>
See also the manual page I linked above for more information.
Note that json_encode() is only available in PHP 5.2 and up, so if you're using an older version, you'll need to use an existing one -- the PHP manual page also includes comments with functions written by people who needed it. (but that said, if you're using anything older than PHP 5.2 you should upgrade ASAP)
Spudley's answer is fine.
Security Notice: The following should not be necessary any longer for you
If you don't have PHP 5.2 you can use something like this:
function js_str($s)
{
return '"' . addcslashes($s, "\0..\37\"\\") . '"';
}
function js_array($array)
{
$temp = array_map('js_str', $array);
return '[' . implode(',', $temp) . ']';
}
echo 'var cities = ', js_array($php_cities_array), ';';
Try this:
const arr = [{"factoryid":233658},{"factoryid":566981},{"factoryid":889566},{"factoryid":114789},{"factoryid":256958},/* more element */];
let newArr = arr.map(elem=>elem.factoryid);
console.log(newArr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
But your mean is code in php or js ?
You need to use an ajax get request to get the data from php to your javascript file. You can use jQuery to make an ajax request like so:
$.ajax(
url: "path_to_your_php_file",
success: data => {
var ordenes = JSON.parse(data);
console.log(ordenes); // Ordenes is now the array in javascript
}
);
In your php file you must "send out" the data you wish the ajax call to receive. You can do this by doing the following:
echo json_encode(array_column($idordenes, 'factoryid'));
Since you're encoding it in the server side, you need to decode it in the client side like:
$chequesArray = Users::all()->toJson();
var dataSet = JSON.parse({!!json_encode($chequesArray)!!});
Or also Using "base64_encode" to conserve the json format like:
$chequesArray = base64_encode(Users::all()->toJson());
var dataSet = JSON.parse(atob('{{$chequesArray}}');
The main difference comes from the use of {{ }} vs {!! !!}, the first one escapes the special chars so it will turn the quotes "" to " then the JS will be unable to parse the string (that why we can use `base64_encode``to conserve the format), the second one will conserve the format and allow the quotes what gives the JS part the ability to parse it simply.
var dataSet = @php $chequesArray @endphp;