Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

194
Views
Get query results in array of arrays using json_encode in php

I am trying to fetch the list of id's associated with mobile number using following code.

$sql = "SELECT id FROM complaints WHERE mobile=1555521555";

$r = mysqli_query($conn,$sql);

$res = mysqli_fetch_array($r);

$result = array();

array_push($result,array(
     "id"=>$res['id']
 )
);

header('Content-Type:application/json');
echo json_encode(array("result"=>$result));

mysqli_close($conn);

But it only result me a single id every time as below instead of list of id's

{"result":[{"id":"82925318"}]}

Actual query result is -

enter image description here

How do I get the multiple results in array of arrays format or in multidimensional array? Like below -

{"result":[{"id":"82925318"}, {"id":"82925319"}]}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need loop to get all data

$result = array();
while($res = mysqli_fetch_array($r))
{
    array_push($result,array(
     "id"=>$res['id']
 )
);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!