if statement using isset当数组中没有任何内容时,我正在尝试创建一个 which 过滤器。该数组大致包含 [0]-[16] 个变量,有时 [11] 有时什么也不包含。当我回声
echo $car['Images'][0]['ImageURI'];
如果 JSON 中有一个变量,它将返回:
https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=1&Width=960
如果里面什么都没有,那么我检索:
Notice: Undefined offset: 0 in /customers/8/9/9/testsite.agency/httpd.www/api/wp-content/themes/divi-child/functions.php on line 94
我希望它能够确定内部是否没有任何内容继续,没有错误/通知。
我的代码:
// Response = to JSON decode this allows us to decode the large response
$response = json_decode($json_data, true);
// Using a foreach to access Nested Array
foreach($response as $index => $car)
{
$car['BasicPrice'];
$car['BodyStyle']['BodyStyle'];
$car['Colour'];
$car['EngineNumber'];
$car['EngineSize'];
$car['FuelType']['FuelType'];
$car['HasServiceHistory'];
// To access the images we have to access a nested array within a nested array.
// This if statement stops the code reading empty car array and throwing an Undefined Index: 0
if( isset($car['Images']) ) {
//it exists
echo $car['Images'][0]['ImageURI'];
echo $car['Images'][1]['ImageURI'];
echo $car['Images'][2]['ImageURI'];
echo $car['Images'][3]['ImageURI'];
echo $car['Images'][4]['ImageURI'];
echo $car['Images'][5]['ImageURI'];
echo $car['Images'][6]['ImageURI'];
echo $car['Images'][7]['ImageURI'];
echo $car['Images'][8]['ImageURI'];
}
}
白板的微信