hereiam
hereiam

Reputation: 836

json_encode with multidimensional array returning string "array" instead of data

I am passing a multidimensional array back to a .php file using json, jquery, and ajax. My goal is essentially to fill a drop down box (id=project) with multiple entries. Here's some code snippets:

$("#turninId").change(function() {

        var user_id = $("#turninId").val();

        $.ajax ( {
          url:"send_input.php",
          type: "POST",
          dataType: "json",
          data:{id_selection: user_id},
          success:function(response) {

            for (var i=0; i<response.proj.length; i++) {
              $("#exp").html(response.proj[i]);
              $("#project").html(response.proj[i]); } });

     });

In send_input.php (backend), I query a db, and send the information to an array. Then I use json_encode.

$query="SELECT project FROM main";
$results = $db->query($query);
while ($row_id = $results->fetchArray()) {
       $proj_option[] = "<option value=\"".$row_id['project']."\">".$row_id['project']."</option>\n";
           $pselected=$row_id['project'];
}
$output = array( "proj" => "$proj_option");
echo json_encode($output);

My problem is that this is RETURNING THE STRING "Array".

For example, if I do: response.proj[0], I get "A" returned.

What gives? I've seen a few people with questions about this error, but no definite solution. Any help?

Upvotes: 0

Views: 551

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

This is because you are casting $proj_option to a string by enclosing it in quotes. Just remove the quotes and you will get the array.

Upvotes: 1

Related Questions