0

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 = [??????];

nkk
  • 43
  • 2
  • 9

2 Answers2

1

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; ?>";
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • it doesn't work.. `var locationsArray = ["";];` – nkk Jun 11 '13 at 05:58
  • you provide the alternate method to encode, but need to assign encoded code to js variable....at the end, i need the variable in this format `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'] ];` – nkk Jun 11 '13 at 06:00
  • @user2471896 see this:: http://stackoverflow.com/questions/5618925/convert-php-array-to-javascript , this should help – Sudhir Bastakoti Jun 11 '13 at 06:04
  • Sorry I am not getting the variable in required format....that just shows assigning the values to js array. – nkk Jun 11 '13 at 06:11
  • please see my answer for the exact outpu...I have found the way now :) – nkk Jun 12 '13 at 19:01
0
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;
nkk
  • 43
  • 2
  • 9