【问题标题】:PHP using json_encode, must output the specific dataPHP使用json_encode,必须输出具体数据
【发布时间】:2019-01-17 08:53:35
【问题描述】:

我目前被困在这种情况下,现在其他开发人员想要输出 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。

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

【问题讨论】:

  • $test_array[] = ['project_name' => $value->project_name, 'unit_types' => $allUnits]; 可能吗?为您提供'project_name' => foo, 'unit_types' => [ foo, bar] 的所需输出

标签: php arrays json object


【解决方案1】:

改变这部分:

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 中不存在)。

【讨论】:

  • 道歉编辑(现在回滚) - 完全忽略了 OP 在执行 array("unit_types"=>$units); 时对 $allunits 所做的事情。很好的答案。
  • 您好,感谢您的回答,它做得很好,但请澄清一下,即使单元类型在数组内抛出单个值? *编辑这是我更改代码时的输出:“all_projects”:[{“project_name”:“ACACIA TOWNHOMES”,“unit_types”:[{“unit”:“TOWNHOUSE 44.00”}]}
  • 这正是您想要的,不是吗?您作为图像附加的 JSON 无效。正如我在回答中所说,JSON 中没有关联数组之类的东西,所以你链接我们的 unit_types 序列化无法完成。
猜你喜欢
  • 2016-06-09
  • 2017-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
相关资源
最近更新 更多