Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestAPI designs in PHP - Create Object of Object instead Array of Objects

Tags:

json

arrays

php

Trying to create an object of object instead my code is creating Array of Objects.

Code to my program

$output['result'] =[];
$query = "SELECT DISTINCT ti.`test_id`, ti.`name`, ti.`subject_id`, ti.`created_timestamp`, ti.`trainer_id`, ti.`class`, ti.`max_marks` , si.`name` as testname, ts.`validity` FROM `test_info` ti, `subject_info` si , `test_schools` ts WHERE ti.`subject_id` = si.`subject_id` AND ts.`test_id` = ti.`test_id` ";
$sth   = $dbh->prepare($query);
if (!$sth->execute()) {
    $dbh = null;
    throw new Exception(-1);
}
$rows = $sth->fetchALL(PDO::FETCH_ASSOC);
foreach($rows as $row){
    $keys = $row['test_id'];
    $output['result'][$keys] = [];  //Creating key 
    array_push($output['result'][$keys],$row); //Provide value to key 
}
echo json_encode($output);

Here I each time I initialize an array in loop because making key as test_id in JSON

$output['result'][$keys] = [];

This lead to array of object, So how I convert into object of object

Output of program

{  
   "result":{  
      "1":[  
         {  
            "test_id":"1",
            "name":"Physics",
            "subject_id":"2",
            "created_timestamp":"2017-03-23 17:55:51",
            "trainer_id":null,
            "class":"8",
            "max_marks":"10",
            "testname":"Physics",
            "validity":"2018-04-14"
         }
      ],
      "2":[  
         {  
            "test_id":"2",
            "name":"phys",
            "subject_id":"2",
            "created_timestamp":"2017-03-24 16:29:33",
            "trainer_id":null,
            "class":"8",
            "max_marks":"5",
            "testname":"Physics",
            "validity":"2017-03-25"
         }
      ]
   }
}

Expected output:

{  
   "result":{  
      "1":{  
         "test_id":"1",
         "name":"Physics",
         "subject_id":"2",
         "created_timestamp":"2017-03-23 17:55:51",
         "trainer_id":null,
         "class":"8",
         "max_marks":"10",
         "testname":"Physics",
         "validity":"2018-04-14"
      },
      "2":{  
         "test_id":"2",
         "name":"phys",
         "subject_id":"2",
         "created_timestamp":"2017-03-24 16:29:33",
         "trainer_id":null,
         "class":"8",
         "max_marks":"5",
         "testname":"Physics",
         "validity":"2017-03-25"
      }
   }
}
like image 749
Sachin Jain Avatar asked Mar 28 '26 14:03

Sachin Jain


1 Answers

There is no need to use array_push, a simple foreach loop will be enough to get it done. In your line of code $output['result'][$keys] = []; you were creating an array and then you were pushing into it, only a assignment can help you achieve the expected result.

Change this to:

foreach($rows as $row){
    $keys = $row['test_id'];
    $output['result'][$keys] = [];  //Creating key 
    array_push($output['result'][$keys],$row); //Provide value to key 
}

This:

foreach ($rows as $row)
{
    $keys = $row['test_id'];
    $output['result'][$keys] =$row ;  //Creating key 
}

Try this code snippet here containing sample inputs

like image 140
Sahil Gulati Avatar answered Mar 30 '26 03:03

Sahil Gulati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!