如何将数组添加到嵌套数组

时间:2018-09-06 06:18:26

标签: php mysql arrays json nested

我的PHP代码如下,我需要一些帮助:

//DB connection
$result = mysqli_query($con,"SELECT * FROM `clients`");
$info = array();
$count = 0;
$meta = array('page' => 1, 'pages' => 1, 'perpage' => -1, 'total' => 14, 'sort' => "asc", 'field' => "ID");

while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

    $info[$row['clientid']]['id'] = $row['id'];
    $info[$row['clientid']]['name'] = $row['name'];
    $info[$row['clientid']]['email'] = $row['email'];
    $info[$row['clientid']]['cell'] = $row['cell'];

    $count++;
}

$data = json_encode(array_values($info));

echo $data;

我的结果;

[{"ID":1,"name":"A","email":"a@a.com","cell":"082"}, 
 {"ID":2,"name":"B","email":"b@b.com","cell":"083"}, 
 {"ID":3,"name":"C","email":"c@c.com","cell":"079"}]

JSON应添加具有以下结果的元数组:

{"meta": 
       {"page": 1,"pages": 1,"perpage": -1,"total": 3,"sort": "asc","field": ID"},
 "data": [{"ID":1,"name":"A","email":"a@a.com","cell":"082"}, 
          {"ID":2,"name":"B","email":"b@b.com","cell":"083"}, 
          {"ID":3,"name":"C","email":"c@c.com","cell":"079"}]
},

1 个答案:

答案 0 :(得分:3)

创建所需结构的数组,然后getUser

json_encode