-2

I'm working on core php,i need how to convert PHP array to Javascript array please help me,below the my example code this there please check it.

I tried from long time to debug but not getting any leads. please help me in resolving this issue

Here my php array data:

Array
(
    [0] => 001-1234567
    [1] => 1234567
    [2] => 12345678
    [3] => 12345678
    [4] => 12345678
)

Here javascript array:

var cities = [
    "Aberdeen",
    "Ada",
    "Adamsville",
    "Addyston",
    "Adelphi"
];
lamp
  • 43
  • 4
  • 5
    Possible duplicate of [Convert php array to Javascript](https://stackoverflow.com/questions/5618925/convert-php-array-to-javascript) – Amit Gupta Mar 14 '18 at 04:50

2 Answers2

0

You can use this script to convert php array to javascript:

<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>
Anas
  • 971
  • 13
  • 28
  • Hi i have this : $locations=array(); $query = $conn->query('SELECT `pg_address` FROM `tbl_master_property`'); while ($row =$query->fetch_assoc()) { $locations[] = $row; } i have to change to javascript array how please – lamp Mar 14 '18 at 05:04
  • Can you use $query->fetch_array() instead of $query->fetch_assoc() ? – Anas Mar 14 '18 at 06:49
0

You can simply convert by JSON encode.

echo json_encode($your_array); 
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
rowmoin
  • 698
  • 2
  • 8
  • 17