PHP如何在另一个列表项中添加列表项

时间:2018-11-28 23:13:16

标签: php json list

我有一份财务发布清单。每次发射可能有也可能没有多次付款。因此,对于每次财务发布,我都会寻找付款清单。我希望此列表在其相应的财务范围内。

我的完整功能:

    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的建议做...但是之后出现了错误

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

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

Error Print

1 个答案:

答案 0 :(得分:0)

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

$_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的区域。