php数组进入javascript数组document.write javascript_array

I have a problem with my javascript array. Bellow my code:

$namen = array();
    $mainimg = array();
    $sql1 = mysql_query("SELECT * FROM image WHERE id = ".$id.";");

        echo "<div class='normal'>";

        while($row = mysql_fetch_array($sql1))
        {
            $namen[] = $row['bild']; 
            $mainimg[] = $row['mainimg'];
            $number_array = count($namen);
            ?>
            <script type='text/javascript'>
            <?php
            for($a=1; $a <= count($namen); $a++){
                $php_array = array($a => $row['bild']);
                $js_array = json_encode($php_array);
                echo "var javascript_array = ". $js_array . ";
";
                echo "document.write(javascript_array + '<br />');";

                }
            ?>
            </script>
        }

Now I get [object][Object] in the browser, but I wanted to printout the elements, the name of the pictures. What can I do?

Thanks for your help in advance.

Regards, Yanick

You're trying to print a Object as a string. What you need to do is to access your properties directly from the object.

<script>
<?php $data = array('foo' => 'bar'); ?>
var my_js_var = <?php echo json_encode($data); ?>;
</script>

With this script, you can access your values like this:

document.write(my_js_var.foo); // writes 'bar'

You'll need to loop through the array and output the fields you want for each item. You're currently printing out the string version of an object in JavaScript, which is always [object Object].