PHP使用json_encode,必须输出特定数据

时间:2019-01-17 08:53:35

标签: php arrays json object

我目前处于这种情况,现在其他开发人员希望输出如所附图片所示的API结构。

json_required_format

但是我尽了最大的努力,但是我只得到了以下结果:

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

我正在使用MVC框架,

这是我的模型,看起来像:

 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();
}

然后我的控制器是:

   $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在我的助手上用于自定义json格式: 这是我的代码:

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)));
}

注意:场景是:       API必须提供所有具有可用单元的项目,     如果项目可用,则需要获取其相应的可用单元进行查看。我知道我可以进行另一个API调用,但是这次,我们需要改进UX。

有人可以启发我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:3)

更改此部分:

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

收件人:

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

您不必像在$allunits = (object) $allunits;中那样将关联数组强制转换为对象,因为关联数组将始终被序列化为JSON对象(关联数组在JSON中不存在)。