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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环
  • ¥15 ADS已经下载好了,但是DAS下载不了,一直显示这两种情况,有什么办法吗,非常急!
  • ¥100 Excel 点击发送自动跳转outlook邮件
  • ¥15 gis中用栅格计算器或加权总和后图层不显示,值也明显不对
  • ¥15 python使用python-pptx如何给幻灯片添加只读密码。
  • ¥15 深度神经网络传递自变量损失