更改JSON的mysql结果以按键索引

时间:2018-08-07 14:27:35

标签: php mysql json

我有一个mysql结果,我将其提取为数组并像这样编码为JSON:

$getDisplayPage = "
SELECT p.id as pageID, page_type_id, display_id, slide_order, duration, background_img, pn.ID as panel_id, panel_type_id, cont_id, c.ID as contID, content 
FROM pages p
inner join panels pn
on p.id = pn.page_id 
inner join content c
on pn.cont_id = c.id
WHERE p.active = 1
and pn.active = 1
AND p.display_id = '".$display."'
";

$showDisplayResult = $mysqlConn->query($getDisplayPage);
while($row=mysqli_fetch_assoc($showDisplayResult))
    {
        $rows[] = $row;
    }
$showDisplays = json_encode($rows);

每个条目返回一行,但是我需要以某种方式将其更改为按页面ID索引。如果您在下面看到我的JSON,它将返回正确的数据,但实际上它应该只有3个对象/行,而不是5。我需要能够访问给定pageID的所有属性。如何更改它以给我正确的JSON对象?

[{"pageID":"93",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"74",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"86",
    "panel_type_id":"2",
    "cont_id":"138",
    "contID":"138",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nLeft 93<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"93",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"74",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"87",
    "panel_type_id":"3",
    "cont_id":"139",
    "contID":"139",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nRight 93<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"95",
    "page_type_id":"1",
    "display_id":"2",
    "slide_order":null,
    "duration":"123",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"90",
    "panel_type_id":"1",
    "cont_id":"142",
    "contID":"142",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nTesting a full page for ID 95<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"105",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"54",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"97",
    "panel_type_id":"2",
    "cont_id":"149",
    "contID":"149",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis is left content<\/p>\r\n<\/body>\r\n<\/html>"},
{"pageID":"105",
    "page_type_id":"2",
    "display_id":"2",
    "slide_order":null,
    "duration":"54",
    "background_img":"images\/bg_rainbow.svg",
    "panel_id":"98",
    "panel_type_id":"3",
    "cont_id":"150",
    "contID":"150",
    "content":"\r\n\r\n\r\n<\/head>\r\n\r\nThis is right content<\/p>\r\n<\/body>\r\n<\/html>"}]

1 个答案:

答案 0 :(得分:0)

---------------------------------
Run id: mnist
Log directory: /tmp/tflearn_logs/
---------------------------------
Training samples: 55000
Validation samples: 55000
--
Training Step: 499  | total loss: 0.12698 | time: 27.880s
| Adam | epoch: 001 | loss: 0.12698 - acc: 0.9616 -- iter: 31936/55000