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

2 Answers 2

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; ?>";
Sign up to request clarification or add additional context in comments.

5 Comments

it doesn't work.. var locationsArray = ["<?php echo $jsoned; ?>";];
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'] ];
@user2471896 see this:: stackoverflow.com/questions/5618925/… , this should help
Sorry I am not getting the variable in required format....that just shows assigning the values to js array.
please see my answer for the exact outpu...I have found the way now :)
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.