dongzhi2887 2015-11-04 04:41
浏览 89
已采纳

PHP为JSON输出创建多维数组

I feel terrible even asking because I have been TRYING to understand and comprehend other peoples examples this is what i'm TRYING to accomplish

{
  "field": [
    {
      "appid": 0,
      "page": 0,
      "fieldname": "Sweet",
      "value": "Tammy Howell"
    },
    {
      "appid": 1,
      "page": 1,
      "fieldname": "Cecilia",
      "value": "Shana Jordan"
    },
    {
      "appid": 2,
      "page": 2,
      "fieldname": "Merritt",
      "value": "Copeland Pena"
    }
  ]
}

I need to be able to make the above JSON output happen when doing an SQL SELECT statement

Here is my currentCode

$x = 0;
$userFieldsResult = mysqli_query($database_id, "SELECT * FROM theDB.DynamicFields ORDER BY Page, Priority") or die (mysqli_error($database_id));

        if (mysqli_num_rows($userFieldsResult)<=0)
            {
                echo "nothing found";
                exit;
            } else 
            {
                while($row = mysqli_fetch_array($userFieldsResult))
                {
                    $userFields[$x]['appid'] = $row['appid'];
                    $userFields[$x]['page'] = $row['page'];
                    $userFields[$x]['fieldname'] = $row['fieldname'];
                    $userFields[$x]['value'] = $row['value'];

                    $x++;
                }
                echo json_encode($userFields);
                exit;
            }

echo json_encode($userFields);

This is normally how i just been outputting json so they each are just part of 1 array looping, but I am trying to understand how to create more "in-depth" JSON arrays so they have a specific identifier before they list out the information.

  [
    {
      "appid": 0,
      "page": 0,
      "fieldname": "Sweet",
      "value": "Tammy Howell"
    },
    {
      "appid": 1,
      "page": 1,
      "fieldname": "Cecilia",
      "value": "Shana Jordan"
    },
    {
      "appid": 2,
      "page": 2,
      "fieldname": "Merritt",
      "value": "Copeland Pena"
    }
  ]

In my example I just want to be able to have "field" be the "upper" part of the array that holds all the information as i showed in the example above.

  • 写回答

2条回答 默认 最新

  • dpaal28266 2015-11-04 04:46
    关注

    In this particular configuration, you are being returned an object with properties, of which, the property's value may be an array.

    So what you should actually be doing is creating a stdClass object in PHP and assigning it a property of "field" which is an empty array. Then you can push into this array stack.

    $obj = new stdClass();
    $obj->fields = array();
    while(....){
        $obj->fields[$x]['appid'] = 
        $obj->fields[$x]['appid'] = $row['appid'];
        $obj->fields[$x]['page'] = $row['page'];
        $obj->fields[$x]['fieldname'] = $row['fieldname'];
        $obj->fields[$x]['value'] = $row['value'];
    }
    

    Now you can json encode the object and return it.

    echo json_encode($obj);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongmu2517 2015-11-04 04:46
    关注

    You need to add field as parent array to your array.

    Change from

    echo json_encode($userFields);
    

    Into

    $json = array('fields' => $userFields);
    echo json_encode($json);
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python词频统计,运行出的Excel没有内容
  • ¥15 求推荐一个好用的录屏软件
  • ¥15 kali显示no x11 display variable was set;
  • ¥15 如何实现这个Python开发问题
  • ¥15 Erasure Code纠删码表
  • ¥15 用vite创建的vue3项目,404重定向不起作用??
  • ¥15 关于#c语言#的问题:一个球从80米高度自由落下,每次落地后反弹的高度为原高度的一半计算6次小球反弹的高度.(反弹结果取整,使用走走for循环结构)
  • ¥15 SurfaceControl的screenshot问题
  • ¥15 基于51单片机的oled菜单代码,要C语言,模块化编程!
  • ¥15 JAVAswing,设计一个扑克牌什么的