Q: how to pass php array as a parameter to the external js function i have sample array $arras below. when we click on the button, the function user_info() will be called and this array will be passed to ajax request.my script looks like
<?php 
   $arr = array(
            array(
               "first_name" => "Darian",
               "last_name" => "Brown",
               "age" => "28",
               "email" => "darianbr@example.com"
          ),
         array(
              "first_name" => "John",
              "last_name" => "Doe",
              "age" => "47",
              "email" => "john_doe@example.com"
         )
      );

   $encoded =json_encode($arr);

?>

<button onclick="user_info()">click here</button>
<div id="result_view"></div>

and my js function is
  function user_info()
  {   
    var my_arr = '<?php echo $encoded; ?>';
    var data='';
    $.ajax({
        data: {arr: my_arr},
        type: 'POST',
        url: site_url+'admin/profile/users/',
        success: function(response)
        {
            var JSONObject = $.parseJSON(response); 
            for (var key in JSONObject) {
                if (JSONObject.hasOwnProperty(key)) {
                   data += (JSONObject[key]["first_name"] + ", " + JSONObject[key]["age"])+"<br/>";
                }
              }
            $('#result_view').html(data);               
          }
      }); 
    }

my php code and js function are on the same php file and it's working. now i want to put this js function to my external js file. and i need to pass my array as a parameter on function call. i tried to pass the array as a parameter like this
<button onclick="user_info(<?php echo $encoded;?>)">click here</button>

and my external js function look like this
function user_info(my_arr)
{
 ...
}

but it's not working.
how can i pass my array as a parameter on external function call?  I would appreciate for any help. thank you.

A: HTML file
<script>
    var enc = <?php echo $encoded;?>;
</script>
<button onclick="user_info()">click here</button>

External JS file
function user_info()
{
    ...
    //use enc var here
    ...
}

Just make sure the external JS file is included after the little script tag.

A: Use single quotes in your array to prevent conflict with double quotes in html attributes. e.g.
   array(
               'first_name' => 'Darian',
               'last_name' => 'Brown',
               'age' => '28',
               'email' => 'darianbr@example.com'
          )


A: Pass it as string
<button onclick="user_info('<?php echo $encoded;?>')">click here</button>


A: You shouldn't put quotes around <?php echo $encoded ?>. That's creating a string instead of an object. So it should be:
var my_arr = <?php echo $encoded; ?>;

