Convert PHP Array to JavaScript

Convert php array to Javascript

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), ';';

How to convert php array to javascript array in php

You can simply convert by JSON encode.

echo json_encode($your_array); 

How do I convert php array of array into js array with using .map to reformat

Thank you everyone for your input... It turns out my array coming out of PHP was not an array, but a string that looked like an array... I would like to take credit for this solution, but I was schooled by an awesome architect I know, and he gave me a single line to solve this. All the code you guys provided worked, except my original array was broken so it wasn't a complete fix. Here is the answer.

var myFences = Array.from(<?php echo json_encode($fences)?>).map(e => eval(e).map(p => ({lat:p[0],lng:p[1]})));

console.log(myFences)

This returns a properly formatted array that I can use to create the right output.

How to convert php array into javascript array to use with the Jquery UI datepicker?

you can use $.parseJSON function.

<script type="text/javascript">
var unavailabledates = $.parseJSON('<?php echo json_encode($disablethese); ?>');
</script>
<script src="js/datepicker-admin.js"></script>

Convert php array to javascript array with select2

You have to transform your data to the correct format, here you can see the correct data format:

var data = [
{
id: 2,
text: '£8, Per hour'
},
{
id: 3,
text: '£10, Per hour'
}
];

You could pass the array in the correct format to the view, something like:

$salaries = \App\Models\Salary::all(); // eloquent collection

$salaries = $salaries->map(function ($salary) {
return ['id' => $salary->id, 'text' => $salary->text];
})->toArray();

It would give result in something like this:

array:1 [▼
0 => array:2 [▼
"id" => 2
"text" => "£8, Per hour"
]
0 => array:2 [▼
"id" => 3
"text" => "£10, Per hour"
]
]

Or you can transform the array in javascript, the Select2 documentation explains here how you can transform your data:

Select2 requires that the id property is used to uniquely identify the
options that are displayed in the results list. If you use a property
other than id (like pk) to uniquely identify an option, you need to
map your old property to id before passing it to Select2.

If you cannot do this on your server or you are in a situation where
the API cannot be changed, you can do this in JavaScript before
passing it to Select2:

var data = $.map(yourArrayData, function (obj) {
obj.id = obj.id || obj.pk; // replace pk with your identifier

return obj;
});

In your case it would be something like this:

$(document).on("change", ".js-selector", function() {

var options = {!! json_encode($salaries) !!};

var data = $.map(options, function (value, key) {
return {id: key, text: value};
});

$("#salary_list").empty().select2({
data: data
});

})

How to convert Php array into a Javascript array and draw in Highcharts

You should use in PHP:

$js = json_encode($obj);

In JavaScript:

var obj = JSON.parse(' ... YOUR JSON STRING FROM PHP ... '); 

==================

Your code:

var obj = new Array('["2019-01-10-12:15,1\r", .... "2019-01-10-12:15,1.3\r"]');

will result nested arrays.



Related Topics



Leave a reply



Submit