0

I am currently printing data from an SQL query using print json_encode($query->result());

Which is giving a result of all the fields in the database. It currently prints :

[{"id":"3812","assigned_to":"1","opportunity_id":null,"lead_stage":"1","first_name":"Jamie","last_name":"Warren","company_name":"","phone_number":"+1","email_address":"[email protected]"},

However I need it to print in the following format :

items": [
    { "id":"3812" 

How can i add the items part?

2 Answers 2

1

You can just json_encode a new array with $query->result() assigned to a key of 'items' like this:

print json_encode(['items' => $query->result()]);
Sign up to request clarification or add additional context in comments.

Comments

0

You can wrap up with an array as like below

print json_encode(array('items' => $query->result()));

OR

$items_arr = array('items' => $query->result());
print json_encode($items_arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.