laravel - 在 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}}
请提出任何解决方案
解决方案
它说未定义的索引,这意味着数组中的键$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
推荐阅读
- angular - 在应用简历上显示主要内容之前打开模式
- javascript - 登录护照js后没有重定向
- python - 使用 PySide2 和 QtQuick 运行 python 文件时出现错误“无法为模块“QtQuick.Window”加载插件
- c# - 通过从组合框中选择类属性名称对列表进行排序
- javascript - 无法使用 .map() 分配给只读属性。使用 NextJs 反冲
- image - 如何使用 ScrollView Image SwiftUI 删除 Zstack 中的顶部安全区域
- java - 使用java在特定点拆分字符串直到特定点
- java - 为什么 Arrays.sort(T[] a, Comparator c) 将 T 推断为 2d 数组的对象?
- python - 计算列表列表中的出现次数python
- android - 如何在 BottomAppBar 中将 CardView 的底部边缘设置为 Fab 形状?