PHP using json_encode, must output the specific data

Viewed 120

I'm currently stuck at this scenario, now the other developer wants to output the API structure as seen on attached image.

json_required_format

But I tried as far as I can but I only got these result:

 "all_projects": {
    "TEST TOWNHOMES": {
        "unit_types": [
            {
                "unit": "TOWNHOUSE 44.00"
            }
        ]
    },
    "TEST HOMES": {
        "unit_types": [
            {
                "unit": "DUPLEX WITH OUT GARAGE 44.50"
            }
        ]
    },
    "TEST HOMES II": {
        "unit_types": [
            {
                "unit": "DUPLEX WITH OUT GARAGE 44.50"
            }
        ]
    },
    "TEST VILLAGE": {
        "unit_types": [
            {
                "unit": "TOWNHOUSE 44.00"
            },
            {
                "unit": "DUPLEX WITHOUT GARAGE 52.30"
            }
        ]
    }

I am using MVC framework,

This is my model looks like:

 public function all_south_projects()
{
    $this->db->distinct();
    return $this->db->select('Project as project_name')->from('lots')
     ->where('available','YES')
    ->get()->result();
}


  public function get_unit_types($projName)
{   
    $this->db->distinct();
    return $this->db->select('UnitType as unit')->from('lots')
    ->where('Project',$projName)
     ->where('Available','YES')
     ->get()->result();
}

And then my controller is:

   $resp = $this->MyModel->all_south_projects();

        $test_array = array();

        foreach ($resp as $value) {

            $units = $this->MyModel->get_unit_types($value->project_name);  
            $allunits = array("unit_types"=>$units);
            $allunits = (object) $allunits;
            $test_array[$value->project_name] = $allunits;
        }

            //var_dump($test_array);
            $stat = 200;
            $message = 'Successfully fetched.';


            if(empty($test_array)){

                $empty=json_decode('{}');
                json_output2($stat,'all_projects',$message,$empty);

                }else{

                json_output2($stat,'all_projects',$message,$test_array);

            }

json_output2 is on my helper to customize json format: Here is my code:

function json_output2($statusHeader,$responseName,$message,$response)
{
    $ci =& get_instance();
    $ci->output->set_content_type('application/json');
    $ci->output->set_status_header($statusHeader);
    $ci->output->set_output(json_encode(array('status' => 
 $statusHeader,'message' => $message,$responseName =>$response)));
}

NOTE: Scenario is: The API must give all the projects having available units, if the project is available, then it needs to get its corresponding available units to view. I know I can make another API call but this time, we need to improve the UX.

Can someone enlighten me to get through this? Thank you!

1 Answers

Change this part :

foreach ($resp as $value) {
    $units = $this->MyModel->get_unit_types($value->project_name);  
    $allunits = array("unit_types"=>$units);
    $allunits = (object) $allunits;
    $test_array[$value->project_name] = $allunits;
}

To :

foreach ($resp as $value) {
    $units = $this->MyModel->get_unit_types($value->project_name); 
    $test_array[] = [
        "project_name" => $value->project_name,
        "unit_types" => $units
    ];
}

You don't have to cast your associative array to object like you did there : $allunits = (object) $allunits; because an associative array will always be serialized as a JSON object (associative arrays do not exist in JSON).

Related