我如何制作嵌套的json格式[重复]

问题描述 投票:-3回答:1
我想在php上写一个基本的api。但我不知道如何制作嵌套的json格式

index.php

<?php require_once __DIR__ . 'config.php'; class API { function Select(){ $db = new Connect; $users = array(); $data = $db->prepare('SELECT * FROM drivers ORDER BY id'); $data->execute(); while($OutputData = $data->fetch(PDO::FETCH_ASSOC)){ $users[$OutputData['id']] = array( 'id' => $OutputData['id'], 'name' => $OutputData['name'], 'station' => $OutputData['station'], 'distance' => $OutputData['distance'], 'score' => $OutputData['score'], 'profile' => $OutputData['profile'], 'comments' => $OutputData['comments'], ); } return json_encode($users); } } $API = new API; header('Content-Type: application/json'); echo API->Select(); ?>

我想将json嵌套在while部分中
php api
1个回答
-1
投票
尝试更改:

function Select() { /* rest of your code */ }

进入:

public function Select() { /* rest of your code */ }

此后,当您创建新的obj:

$API = new API();

记住选择是函数。

echo $API->Select();

© www.soinside.com 2019 - 2024. All rights reserved.