【问题标题】:PHP json_encodePHP json_encode
【发布时间】:2015-01-19 16:00:24
【问题描述】:

这是我目前的脚本:

 //... PDO CONNECTION AND QUERY //...*

 $result = $dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);

 foreach ($result as $row){
     $return[]=array('employeeid'=>$row['employeeid'],
                        'firstname'=>$row['firstname'],
                        'lastname'=>$row['lastname'],
                        'id'=>$row['id'],
                        'startdate'=>$row['startdate'],
                        'enddate'=>$row['enddate'],
                        'type'=>$row['type'],
                        'reason'=>$row['reason']);
 }     
 $dbh = null;

 header('Content-type: application/json'); echo '' .
 json_encode($return) .'';

这给出了如下结果:

[
    {
        'employeeid': '1',
        'firstname': 'john',
        'lastname': 'doe',
        'id': '001',
        ...,
        ...
    }
]

[
    {
        'employeeid': '1',
        'firstname': 'john',
        'lastname': 'doe',
        'id': '002',
        ...,
        ...
    }
]

但我想要的是这样的结果(所以每个员工都有一个带有多个请求的对象):

[
    {
        'employeeid': '1',
        'firstname': 'john',
        'lastname': 'doe',
        'requests': [
            {
                'id': '001',
                ...,
                ...
            },
            {
                'id': '002',
                ...,
                ...
            }
        ]
    }
]

有人可以帮我解决这个问题吗?

提前致谢,

一月

【问题讨论】:

  • 你的循环有什么意义?
  • 那么您需要以这种方式构建您的阵列。
  • 请出示您的查询。
  • @Brewal 我的猜测是限制返回的列(也许他通过SELECT * 获取500 列),而不是简单地在数据库查询中指定所需的列。
  • 你的 js 应该解码响应......并且请求应该是在服务器端形成的数组

标签: php mysql json pdo


【解决方案1】:

如下更改您的foreach

foreach ($result as $row){
    $employeeid = $row['employeeid'];
    if (!isset($return[$employeeid]))
        $return[$employeeid] = array(
            'employeeid' => $row['employeeid'],
            'firstname' => $row['firstname'],
            'lastname' => $row['lastname'],
            'requests' => array()
        );

    $return[$employeeid]['requests'][] = array(
        'id' => $row['id'],
        'startdate' => $row['startdate'],
        'enddate' => $row['enddate'],
        'type' => $row['type'],
        'reason' => $row['reason']
    );
}     

【讨论】:

    【解决方案2】:

    您需要在每个员工数组中包含 requests 记录:

    foreach ($result as $row){
        $employee=array('employeeid'=>$row['employeeid'],
                            'firstname'=>$row['firstname'],
                            'lastname'=>$row['lastname'],
                            'id'=>$row['id'],
                            'startdate'=>$row['startdate'],
                            'enddate'=>$row['enddate'],
                            'type'=>$row['type'],
                            'reason'=>$row['reason']);
         $requests = $dbh->query(sprintf("SELECT * FROM employee_requests WHERE employee_id = '%s'", $row['employeeid']))->fetchAll(PDO::FETCH_ASSOC);
         $employee['requests'] = requests;
         $return[] = $employee;
     } 
    

    【讨论】:

      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2012-11-09
      • 2011-05-03
      • 2017-06-17
      相关资源
      最近更新 更多