Reputation: 43
I have an array {$address = array(..........);
}
I have converted it like
{
foreach ($address as $key => $val){
$points[] = "['{$val}', '{$val}']";
}
$output = join ("," , $points);
$req_format = strip_tags($output);
}
which outputs: ['30 South Wacker Drive Floor 22 Chicago IL 60606 ', '30 South Wacker Drive Floor 22 Chicago IL 60606 '],['288 Bishopsgate London, EC2M 4QP United Kingdom ', '288 Bishopsgate London, EC2M 4QP United Kingdom '],['260 Madison Avenue 8th Floor New York NY 10016 ', '260 Madison Avenue 8th Floor New York NY 10016 ']
I need to assign this value to a js variable:
var locationsArray = [
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606'],
['30 South Wacker Drive Floor 22 Chicago IL 60606', '30 South Wacker Drive Floor 22 Chicago IL 60606']
];
how can I assign php variable $req_format equal to js variable locationArray = [??????];
Upvotes: 0
Views: 159
Reputation: 43
foreach ($address as $key => $val){
$val = strip_tags($val);
$points[] = array($val, $val);
}
$json = json_encode($points);
$json = str_replace('\n',' ',$json);
$json = str_replace('\r',' ',$json);
var locations = '<?php echo $json;?>';
var locations_array = JSON.parse(locations);
var locationsArray = locations_array;
Upvotes: 0
Reputation: 100175
you could use json instead, like
foreach ($address as $key => $val){
$points[] = "['{$val}', '{$val}']";
}
$jsoned = json_encode($points);
//pass it to your js
var js_data = "<?php echo $jsoned; ?>";
Upvotes: 1