尝试循环数组

时间:2017-09-02 19:03:55

标签: php arrays json laravel-5

我是JSON响应主体然后我将其转换为数组使用json_decode()但是当我尝试循环一个单个数组时我收到了错误

  

非法字符串偏移

这是我的json回复

array(5) { ["StartDate"]=> string(10) "2016-08-29" ["EndDate"]=> string(10) "2016-09-01" ["Currency"]=> string(3) "IDR" ["StartBalance"]=> string(12) "100000000.00" ["Data"]=> array(20) { [0]=> array(6) { ["TransactionDate"]=> string(5) "29/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(10) "9000000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING DB" ["Trailer"]=> string(58) "2808/ACDFT/WS950519000000.00 REK KORAN DARI GIRO KE TAPRES" } [1]=> array(6) { ["TransactionDate"]=> string(5) "29/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(8) "25000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING CR" ["Trailer"]=> string(26) "08/28 95031 FOR CORPDUMMY1" } [2]=> array(6) { ["TransactionDate"]=> string(5) "29/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(9) "200000.00" ["TransactionName"]=> string(17) "BA JASA E-BANKING" ["Trailer"]=> string(36) "2908/TRCHG/WS95051BIAYA TRANSFER SME" } [3]=> array(6) { ["TransactionDate"]=> string(5) "29/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(10) "1000000.00" ["TransactionName"]=> string(17) "BYR VIA E-BANKING" ["Trailer"]=> string(51) "29/08 WSID95051 PENERIMAAN NEGARA 115110002341111" } [4]=> array(6) { ["TransactionDate"]=> string(5) "29/08" ["BranchCode"]=> string(4) "0061" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(9) "900000.00" ["TransactionName"]=> string(13) "SETORAN TUNAI" ["Trailer"]=> string(14) "PEMBUKAAN REK." } [5]=> array(6) { ["TransactionDate"]=> string(5) "30/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(8) "10000.00" ["TransactionName"]=> string(17) "BA JASA E-BANKING" ["Trailer"]=> string(36) "3008/TRCHG/WS95051BIAYA TRANSFER SME" } [6]=> array(6) { ["TransactionDate"]=> string(5) "30/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(10) "5000000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING DB" ["Trailer"]=> string(61) "3008/FTRTG/00001000110042 other bank TRANSFER VIA RTGS Dummy4" } [7]=> array(6) { ["TransactionDate"]=> string(5) "30/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(11) "70000000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING CR" ["Trailer"]=> string(53) "3008/FTSCY/WS95051 70000000.00 Ticket Payment DUMMY13" } [8]=> array(6) { ["TransactionDate"]=> string(5) "30/08" ["BranchCode"]=> string(4) "0061" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(10) "5000000.00" ["TransactionName"]=> string(8) "NK - LLG" ["Trailer"]=> string(0) "" } [9]=> array(6) { ["TransactionDate"]=> string(5) "30/08" ["BranchCode"]=> string(4) "0998" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(9) "800000.00" ["TransactionName"]=> string(15) "SETORAN VIA CDM" ["Trailer"]=> string(25) "3008 WSID:Z9991 DUMMY16" } [10]=> array(6) { ["TransactionDate"]=> string(5) "31/08" ["BranchCode"]=> string(4) "0015" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(10) "1000000.00" ["TransactionName"]=> string(13) "NK - KU MASUK" ["Trailer"]=> string(0) "" } [11]=> array(6) { ["TransactionDate"]=> string(5) "31/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(8) "30000.00" ["TransactionName"]=> string(9) "BIAYA ADM" ["Trailer"]=> string(0) "" } [12]=> array(6) { ["TransactionDate"]=> string(5) "31/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(8) "11100.00" ["TransactionName"]=> string(5) "BUNGA" ["Trailer"]=> string(0) "" } [13]=> array(6) { ["TransactionDate"]=> string(5) "31/08" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(7) "2220.00" ["TransactionName"]=> string(11) "PAJAK BUNGA" ["Trailer"]=> string(0) "" } [14]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(9) "100000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING DB" ["Trailer"]=> string(56) "0109/FTSCY/WS95051 100000.00 Online Transfer PT DUMMY2" } [15]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0061" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(10) "3000000.00" ["TransactionName"]=> string(8) "NK - LLG" ["Trailer"]=> string(0) "" } [16]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(9) "250000.00" ["TransactionName"]=> string(17) "TRSF E-BANKING DB" ["Trailer"]=> string(44) "0109/FTSCY/WS95051 250800.00 Transfer DUMMY1" } [17]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0000" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(9) "100000.00" ["TransactionName"]=> string(17) "BA JASA E-BANKING" ["Trailer"]=> string(36) "0109/TRCHG/WS95051BIAYA TRANSFER SME" } [18]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0101" ["TransactionType"]=> string(1) "C" ["TransactionAmount"]=> string(8) "10000.00" ["TransactionName"]=> string(11) "KR OTOMATIS" ["Trailer"]=> string(20) "DUMMY7 039903811112" } [19]=> array(6) { ["TransactionDate"]=> string(4) "PEND" ["BranchCode"]=> string(4) "0038" ["TransactionType"]=> string(1) "D" ["TransactionAmount"]=> string(9) "100000.00" ["TransactionName"]=> string(13) "TARIKAN TUNAI" ["Trailer"]=> string(0) "" } } }

这个JSON to array

$output = curl_exec($ch); // This is API Response
        curl_close($ch);
        $result = json_decode($output,true);

        return view("bca.cekmutasi", [
            "result" => $result
            ]);

这是我的观点

@if (isset($result))
@foreach ($result as $key => $value)
<div class="col-xs-6">
  <ul class="list-unstyled" style="line-height: 2">
  <li><span class="fa fa-circle text-success"></span> TransactionDate : <b>{{ $value['StartDate']['EndDate']['Currency']['StartBalance']['StartBalance']['Data'][0]['TransactionDate'] }}</b></li>
  </ul>
</div>
@endforeach
@endif

如果我只是放{{ $result['Data'][0]['TransactionDate'] }}而不预先显示结果

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的回复不是数组,您需要在foreach上执行此操作

@if (isset($result))
@foreach ($result['Data'] as $key => $value)
<div class="col-xs-6">
  <ul class="list-unstyled" style="line-height: 2">
  <li><span class="fa fa-circle text-success"></span> TransactionDate : <b>{{ $value['TransactionDate'] }}</b></li>
  </ul>
</div>
@endforeach
@endif

答案 1 :(得分:0)

你必须这样关闭它  

{
    "StartDate": "2016-09-01",
    "EndDate": "2016-09-01",
    "Currency": "IDR",
    "StartBalance": "94163880.00",
    "Data": [{
            "TransactionDate": "PEND",
            "BranchCode": "0000",
            "TransactionType": "D",
            "TransactionAmount": "100000.00",
            "TransactionName": "TRSF E-BANKING DB",
            "Trailer": "0109/FTSCY/WS95051 100000.00 Online Transfer   PT DUMMY2"
        },
        {
            "TransactionDate": "PEND",
            "BranchCode": "0061",
            "TransactionType": "C",
            "TransactionAmount": "3000000.00",
            "TransactionName": "NK - LLG",
            "Trailer": ""
        }
    ]
}