简体   繁体   English

具有多维数组的json_encode返回字符串“ array”而不是数据

[英]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. 我正在使用json,jquery和ajax将多维数组传递回.php文件。 My goal is essentially to fill a drop down box (id=project) with multiple entries. 我的目标实质上是用多个条目填充一个下拉框(id = project)。 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. 在send_input.php(后端)中,我查询一个db,并将信息发送到数组。 Then I use json_encode. 然后我使用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. 例如,如果执行以下操作: response.proj[0] ,则返回“ A”。

What gives? 是什么赋予了? I've seen a few people with questions about this error, but no definite solution. 我见过一些人对此错误有疑问,但没有确定的解决方案。 Any help? 有什么帮助吗?

This is because you are casting $proj_option to a string by enclosing it in quotes. 这是因为您通过将$proj_option括在引号中而将其$proj_option转换为字符串。 Just remove the quotes and you will get the array. 只需删除引号,您将获得数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM