【问题标题】:Illegal String offset when trying to loop array尝试循环数组时出现非法字符串偏移
【发布时间】:2018-02-11 12:31:11
【问题描述】:

我有 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 到数组

$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'] }} 而不使用 foreach 它会显示结果

我该如何解决这个问题?

【问题讨论】:

    标签: php arrays json laravel-5


    【解决方案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
    

    【讨论】:

    • 它不是对象。我用了你的答案然后我尝试了非对象的属性
    • 更新了答案以反映访问它的正确方法
    • 你的答案是正确的,我试过了,我有很多循环
    【解决方案2】:

    你必须像这样关闭它

    { "开始日期": "2016-09-01", "结束日期": "2016-09-01", “货币”:“印尼盾”, "StartBalance": "94163880.00", “数据”: [{ “交易日期”:“待定”, "分行代码": "0000", “交易类型”:“D”, "TransactionAmount": "100000.00", "TransactionName": "TRSF E-BANKING DB", 《预告片》:《0109/FTSCY/WS95051 100000.00 在线转账 PT DUMMY2》 }, { “交易日期”:“待定”, "分行代码": "0061", “交易类型”:“C”, "TransactionAmount": "3000000.00", "TransactionName": "NK - LLG", “预告片”: ”” } ] }

    【讨论】:

    • 很抱歉,但 } 只是包含在内。我的错,我已经编辑了问题
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2019-03-01
    • 2020-08-17
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多