How to add an array to a nested array

How to add an array to a nested array



My PHP Code is the following, I need some assistance please:


//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;



My Result;


["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"]



The JSON should add the meta array with the following result:


"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 Answer
1



Create array of required structure and json_encode it:


json_encode


$data = json_encode(array(
'meta' => $meta,
'data' => array_values($info),
));






Thanks u_mulder for the quick response, it works 100%!

– Janp
Sep 6 '18 at 6:28



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.