Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have this PHP which is fetching all rows of address column from the database and i an using json_encode() function to convert it into string and JSON.parse in javascript but i am not getting output as expected

 <? 

 include('dbcon.php');
 $result = mysql_query("SELECT address FROM markers");

 while ($row = mysql_fetch_assoc($result)) {
 $new_array[] = $row; 
 }
 print_r($new_array);

 $add_js = json_encode( $new_array );

 print_r($add_js);
 ?>

with print_r($new_array); i am getting two dimentional arrary and getting null in zeroth location. any changes i make it keeps on giving null at zeroth location.

   var address = [JSON.parse( '<?php echo $add_js ?>' )];

var address should store array of addresses but it is giving output when i alert in the array.

[object Object],[object Object],[object Object],[object Object],[object Object]

i want to store array of addresses after fetching them from database

share|improve this question
    
Can you please post the results which your PHP generates? –  Bergi Oct 26 '13 at 12:18
    
First of all initialize the $new_array by adding $new_array = arary() at the begining of you code. Then alert won't display json string, it is an object, rather than using alert() for debuging, add console.log(address); and checkout your browser console (f11). –  Filip Górny Oct 26 '13 at 12:20
    
What's wrong with that array of objects, what did you expect ([object Object] is OK for an object that gets stringified from an alert)? Are you sure that you need the brackets (array literal) around the JSON.parse expression? Also, you should not need to use a string and explicit parse, you can simply echo the JSON as a javascript literal into the code. –  Bergi Oct 26 '13 at 12:21
1  
results of php are ' Array ( [0] => Array ( [address] => Janak Puri, New Delhi, India ) [1] => Array ( [address] => Sector 63, Noida ) [2] => Array ( [address] => Dwarka, New Delhi, India ) [3] => Array ( [address] => Laxmi Nagar,new delhi ) [4] => Array ( [address] => Gurgaon, Haryana, India ) ) [{"address":"Janak Puri, New Delhi, India"},{"address":"Sector 63, Noida"},{"address":"Dwarka, New Delhi, India"},{"address":"Laxmi Nagar,new delhi"},{"address":"Gurgaon, Haryana, India"}] ' these are results fron temporary database. –  agnes Oct 26 '13 at 12:22
    
Why are you not just using var address = <?php echo json_encode($some_variable);?>;? –  Wrikken Oct 26 '13 at 12:26

2 Answers 2

up vote 4 down vote accepted

So $new_array is an array of associative arrays, and it is decoded in JavaScript as an array of objects. If you want an array of strings instead, then you need to store strings in $new_array:

$new_array = array();
while ($row = mysql_fetch_assoc($result)) {
   $new_array[] = $row['address'];   // <-- This line
}

print_r($new_array);

$add_js = json_encode( $new_array );

print_r($add_js);
share|improve this answer
1  
code is working. thanks :) –  agnes Oct 26 '13 at 12:50

I think this is enough for your need:

var address = <?php echo $add_js ?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.