跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • php如何生成这样的json格式呢?
未分類
11 5 月 2020

php如何生成这样的json格式呢?

hihelllo 大佬有话说 :

php如何生成这样的json格式呢?

{
        "hello":100,
        "data":[{
                "id":1,
                "title":"我是标题1",
                "content":"我是内容1"
        },{
                "id":2,
                "title":"我是标题2",
                "content":"我是内容2"
        },{
                "id":3,
                "title":"我是标题3",
                "content":"我是内容3"
        }]
}

完全不会这样的格式。。。

Mr. 大佬有话说 :

本帖最后由 Mr. 于 2020-5-10 23:52 编辑

<?php

$arr = [
"hello" => 100,
"data" => [
    [
      "id" => 1,
      "title" => "我是标题1",
      "content" => "我是内容1"
    ],
    [
      "id" => 2,
      "title" => "我是标题2",
      "content" => "我是内容2"
    ],
    [
      "id" => 3,
      "title" => "我是标题3",
      "content" => "我是内容3"
    ]
]
];

echo json_encode($arr);

camboo 大佬有话说 :

json看起来是后端的东西 不懂

JOSH 大佬有话说 :

多维数组 转json对象

https://cdn.jsdelivr.net/gh/hishis/forum-master/public/images/patch.gif

tir 大佬有话说 :

这不是josn嘛 哈哈

hihelllo 大佬有话说 :

Mr. 大佬有话说 : 2020-5-10 23:50

mysql_fetch_array查出数据
$a[]='{"id":’.$rows.’,"code":"’.$rows.’","type":"’.$rows.’"},’;
。。。。
$contentStr = implode(”,$a);
        $myjson='{"hello":100,"data":[‘.$contentStr.’]}’;
                header(‘content-type:application/json’);
                echo str_replace(‘,]’,’]’,$myjson);

我现在是手动组合起来打印。。。哈哈:L

hihelllo 大佬有话说 :

JOSH 大佬有话说 : 2020-5-10 23:47
多维数组 转json对象

现在手动组合凑活用,str_replace替换掉最后一个, 哈哈

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具