首页 > 解决方案 > 在 laravel 5.6 中我的刀片视图有一个未定义的索引问题

问题描述

我在 laravel 5.6 中的刀片视图中有一个未定义的索引问题。我必须在收到错误时运行我的 URL localhost:8000/test Undefined index: Domains。如果在 URL 中传递一些值,例如 localhost:8000/test?tld=test&sld=info 它工作正常。请提出任何解决方案。

我的查看页面代码

@foreach($result['Domains']['Domain'] as $key => $value)
      @if($key == 'Name')
      <b>{{$value}}</b> - 
      @endif
      @if($key == "RRPText")
      <b>{{$value}}</b>
      @endif
      @endforeach
      @foreach($result['Domains']['Domain']['Prices'] as $key => $value)
      @if($key == "Registration")
      <b>{{$value}}</b>
      @endif 
      @endforeach

我的控制器代码

$sld = $request['sld'];
        $tld = $request['tld'];
        $response = file_get_contents('https://reseller.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml&version=2&includeprice=1&includeproperties=1&includeeap=1');  
        $data = simplexml_load_string($response);
        $configdata   = json_encode($data);
        $final_data = json_decode($configdata,true);

我的 API 调用输出

{"interface-response":
{"Domains":
{"Domain":
{"Name":"decksys.info","RRPCode":"210","RRPText":"Domain available","IsPremium":"False","IsPlatinum":"False","IsEAP":"False","Prices":{"Currency":"","Registration":"12.48","Renewal":"12.48","Restore":"250.00","Transfer":"12.48","ExpectedCustomerSuppliedPrice":null}}},"Command":"CHECK","APIType":"API.NET","Language":"eng","ErrCount":"0","ResponseCount":"0","MinPeriod":"1","MaxPeriod":"10","Server":"sjl0vwapi08","Site":"eNom","IsLockable":null,"IsRealTimeTLD":null,"TimeDifference":"+0.00","ExecTime":"0.553","Done":"true","TrackingKey":"a1c38f08-5042-4139-a525-302d987a2b39","RequestDateTime":"5/25/2018 4:23:31 AM","debug":null}}

请提出任何解决方案

标签: laravellaravel-5laravel-4laravel-5.2

解决方案


它说未定义的索引,这意味着数组中的键$result['Domains']['Domain']$result['Domains']['Domain']['Prices']不存在。

尝试转储你的$results变量,dd($results)在 foreach 之前使用,你会发现没有关键。

您可以检查isset()该数组中是否存在索引

@if(isset($result['Domains']['Domain'])) 
    @foreach($result['Domains']['Domain'] as $key => $value)
      @if($key == 'Name')
        <b>{{$value}}</b> - 
      @endif
      @if($key == "RRPText")
        <b>{{$value}}</b>
      @endif
    @endforeach
@endif
@if(isset($result['Domains']['Domain']['Prices']))
    @foreach($result['Domains']['Domain']['Prices'] as $key => $value)
      @if($key == "Registration")
        <b>{{$value}}</b>
      @endif 
    @endforeach
@endif

您可以在此处了解有关 PHP 数组的更多信息:https ://www.w3schools.com/php/php_arrays.asp


推荐阅读