duanqianwei2485
2019-07-04 18:51
浏览 55
已采纳

我该如何处理?

Let´s see some code to explain better: I made a request to a WS as showing bellow:

        $idERP = 1;
        $grupoEcota = new \SoapClient($url);
        $paramExtrato = array(
            'sGrupo' => $request->sGrupo,
            'iCota' => $request->iCota,
            'iIdERP' => $idERP,
        );
        $responseExtrato = $grupoEcota->ROS_ExtratoContaCorrente($paramExtrato);
        $grupoEcota = $responseExtrato->ExtratoContaCorrente;

I get a response from a WS witch looks like this from dd(die and dump):


$dados[] = $grupoEcota->{'PARCELAS-PAGAS'};
        dd($dados);

array:1 [▼
  0 => {#784 ▼
    +"PARCELA-PAGA": array:21 [▼
      0 => {#783 ▼
        +"NUMERO-PARCELA": 48
        +"DATA-VENCIMENTO": "20170818"
        +"DATA-PAGAMENTO": "20170919"
        +"VALOR-PARCELA": 580.28
        +"HITORICO-PARCELA": ""
        +"PERC-PAGO": 3.491
      }
      1 => {#771 ▶}
      2 => {#781 ▶}
      3 => {#780 ▶}
      4 => {#779 ▶}
      5 => {#778 ▶}
      6 => {#777 ▶}
      7 => {#776 ▶}
      8 => {#775 ▶}
      9 => {#774 ▶}
      10 => {#773 ▶}
      11 => {#772 ▶}
      12 => {#757 ▶}
      13 => {#770 ▶}
      14 => {#769 ▶}
      15 => {#768 ▶}
      16 => {#767 ▶}
      17 => {#766 ▶}
      18 => {#765 ▶}
      19 => {#764 ▶}
      20 => {#763 ▶}
    ]
  }
]

I need to get only the first "DATA-VENCIMENTO": "20170818" item. How do I do it, please?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drtoaamk20278 2019-07-04 19:54
    已采纳

    Firstly you are dealing with an array, lets fetch that first element out (my assumption is there will only be one element.).

    $data= $data[0];
    

    Now we have a key value array, we can now fetch out the PARCELA-PAGA.

    $parcels = $data{'PARCELA-PAGA'];
    

    Multiple objects in a array is the next level, this means that there are in each object data, so i would use array_pluck to get these elements out.

    $vencimento = array_pluck($parcels, 'DATA-VENCIMENTO');
    

    If you do not care about multiple data fields, you could simply take the first element. Be careful this data is not an array, but an object.

    $vencimento = $parcels[0]->DATA-VENCIMENTO;
    

    Which should result in a result looking like this.

    [""20170919"", "20171019", ...]
    
    点赞 打赏 评论
  • drxdn40242 2019-07-05 13:47

    Got the help of a friend:

    $firstRecord = collect($dados[0]->{'PARCELA-PAGA'})->firstWhere('DATA-VENCIMENTO', '20170818');
    
    dd($firstRecord);
    

    then...

    {#783 ▼
      +"NUMERO-PARCELA": 48
      +"DATA-VENCIMENTO": "20170818"
      +"DATA-PAGAMENTO": "20170919"
      +"VALOR-PARCELA": 580.28
      +"HITORICO-PARCELA": ""
      +"PERC-PAGO": 3.491
    }
    

    And I finished doing:

    dd($firstRecord->{'DATA-VENCIMENTO'});
    

    I got "20170818" which is the value I'm looking into. I think it solves the problem! Thanks, dude, you are the man!

    点赞 打赏 评论