【问题标题】:PHP how to add a list items inside another list itemsPHP如何在另一个列表项中添加列表项
【发布时间】:2018-11-28 23:13:15
【问题描述】:

我有一份财务启动清单。每次发射可能有也可能没有多次付款。因此,对于每次财务启动,我都会查找付款清单。我希望此列表包含在其相应的财务发布中。

我的完整功能:

    public function customerInvoice()
    {
        $_customer = filtra_int($this->input->post('cliente_id'));
        $this->redireciona_id_nula($_customer, $this->url . '/ficha');
        $_view = [];
        $this->load->helper(['filter', 'string', 'currency', 'validate', 'phone']);

        $_view['glyph'] = 'user';
        $_view['enterprise'] = $this->enterprise;
        $_view['buttons'] = $this->_getFormButtons($_customer, $this->url . '/ficha');

        $this->load->model('lancamento_model', 'lancamentoModel');
        $_view['releases'] = $this->lancamentoModel->getCustomerRelease($_customer);
        foreach ($_view['releases'] as $i => $value) {
//            $_view['releases']['order'][$i] = $this->lancamentoModel->getCustomerOrder($value->id);
             $value['order'] = $this->lancamentoModel->getCustomerOrder($value->id);
        }

        $this->addBreadcrumb('Lançamentos', base_url() . 'lancamentos');
        $this->addBreadcrumb('Ficha', base_url() . 'lancamentos/ficha');
        $this->addBreadcrumb('Exibir');

        $this->extras['css'][] = $this->load->view('lancamentos/consulta/ficha_cliente/form/css', null, true);
        $this->extras['scripts'][] = $this->load->view('lancamentos/consulta/ficha_cliente/form/js', $_view, true);

//        $pagina = $this->getHtmlPagina('Ficha cliente', $_view);
//        $this->load->view('lancamentos/consulta/ficha_cliente/view/view', $pagina);


        $this->output->set_content_type('application/json');
        $this->output->set_output(json_encode($_view));
    }

json 结果:

 {  
   "release":{  
      "0":{  
         "id":"380",
         "data_vcto":"2016-01-15",
         "data_emissao":"2016-01-15",
         "documento":"292\/1",
         "vlr":"67.00",
         "vlr_divida":"0.00"
      },
      "order":[  
         [  
            {  
               "id":"142206",
               "data_vcto":"2016-01-15 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"67.00",
               "tipo_pagamento_nome":"Dinheiro"
            },
            {  
               "id":"213",
               "data_vcto":"2016-01-13 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"22.00",
               "tipo_pagamento_nome":"Dinheiro"
            }
         ]
      ]
   }
}

我想要这样的东西

{  
   "release":{  
      "0":{  
         "id":"380",
         "data_vcto":"2016-01-15",
         "data_emissao":"2016-01-15",
         "documento":"292\/1",
         "vlr":"67.00",
         "vlr_divida":"0.00",
         "order":[  
            {  
               "id":"142206",
               "data_vcto":"2016-01-15 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"67.00",
               "tipo_pagamento_nome":"Dinheiro"
            },
            {  
               "id":"213",
               "data_vcto":"2016-01-13 09:59:24",
               "vlr_desconto":"0.00",
               "vlr_multa":"0.00",
               "pago_em":"2018-11-19 09:59:24",
               "conta_nome":"Caixa",
               "tipo_nome":"Vendas",
               "vlr_movimento":"22.00",
               "tipo_pagamento_nome":"Dinheiro"
            }
         ]
      }      
   }
}

有可能吗?在 foreach 中我必须做什么?

更新:我按照@Yulio Aleman Jimenez 的建议做了...但之后出现了错误

致命错误:无法在 C:\xampp\htdocs\beauty\application\controllers\Lancamentos.php 第 829 行将 stdClass 类型的对象用作数组

消息:不能将 stdClass 类型的对象用作数组

Error Print

【问题讨论】:

    标签: php json list


    【解决方案1】:

    我认为您必须像这样更改代码:

    $_releases = $this->lancamentoModel->getCustomerRelease($_customer);
    foreach ($_releases as $i => $value) {
        // try with this changes
        $value = $this->lancamentoModel->getCustomerOrder($value->id);
        $_releases[$i] = (object)array_merge((array)$_releases[$i], array('order' => $value));
    }
    

    问题是将订单数组存储在每个版本的order 中。

    更新

    您正在处理对象,这是您必须转换为 array、在 order 中添加新值然后转换为 object 的原因。

    【讨论】:

    • 我收到此错误:消息:无法修改标头信息 - 标头已发送(输出开始于 C:\xampp\htdocs\beauty\application\controllers\Lancamentos.php:829)。第 829 行是您的代码..
    • 你能显示第 829 行周围的代码部分吗?似乎该错误与您最初问题的代码无关。
    • 查看此答案以找到可能的解决方案stackoverflow.com/questions/8028957/…,更新您的问题以获得更多帮助或提出另一个新问题。
    • 我更新了我的问题...之前没有错误。我只是希望我的 json 采用我所说的格式。按照您的建议进行操作后,错误开始出现。有什么建议吗?
    • 我解决了我的问题:foreach ($_view['releases'] as $i => $value) { $_view['releases'][$i]->payments = $this-> lancamentoModel->getCustomerOrder($value->id); }。感谢支持!
    猜你喜欢
    • 2021-08-18
    • 2019-09-22
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 1970-01-01
    • 2017-09-13
    • 2021-10-21
    相关资源
    最近更新 更多