Q:

Pass PHP array to Javascript inside a string variable (HTML Injection)

I know this is a common question, but I am struggling with this!
I'm creating the HTML from a PHP file, so every HTML and Javascript is being passed as a variable ($string). This is some of my code, where I need to pass the values of $objects_list to the javascript variable objects_list:
    $string = '<script type="text/javascript">
                function open_project(id)
                {
                    var objects_list = '. json_encode($objects_list) .';
                    console.log(objects_list.lenght);
                }
            </script>';

Thing is, that console.log prints undefined. How can I solve this?
If I remove the json_encode I get an error for converting array to string.

EDIT:
Just noticed, if I console.log(objects_list[5], by example, it prints the correct value so the problem is actually with the objects_list.lenght I guess.

A:

Spelling mistake:
console.log(objects_list.lenght);

Should be 
console.log(objects_list.length);

