Question: I'm trying to create an object in javascript with PHP. This is my script:

```
    <script>
        $(document).ready(function(){
            var genres = [
    <?php
    $last = count($userGenres) - 1;
    $i = 0;
    foreach($userGenres as $a){
        $i++;
        echo '{"value":"'.$a['genre'].'", "name":"'.$a['name'].'"}';
        if($i < $last){
            echo ',';
        }
    }
    ?>
                        ];
   });
</script>

```

When I check the generated source, it creates a valid object, but the whole script in that tag doesn't work now. How would fix this, without JSON?

Thanks

Answer: 
As the comment says you should consider using (php function)json\_encode instead, which will turn...

```
php:

$a = array(
  array("gender"=> "male", "name"=> "Bob"),
  array("gender"=> "female", "name"=> "Annie")
);

Into json:
[
   {gender:"male", name:"Bob"},
   {gender:"female", name:"Annie"}
]

```

Echoing json\_encode($a) would output it just like that.
Your function is not closed properly:

```
}
    }
    ?>
                        ];
}); // <--- add this
</script>

```