我最近在 Stack - Search array for specific string上发布了这个问题- 并得到了我的问题的完美答案,我将其标记为正确。但是我现在有一个相关的问题,我发现很难解决,但看起来应该很容易!
我使用以下内容在数组中搜索了字符串“GB”和“US”,这是在上一篇文章中提供给我的:
/* Get Movie release dates */
$releases = $tmdb->getMovieReleases($tmdb_id);
echo "<pre>";
print_r($releases);
echo "</pre>";
$uk_release = -1;
foreach($releases['countries'] as $k=>$v) {
if(array_search('GB', $v)) {
$uk_release = $k;
break;
} else {
$uk_release = null;
}
}
$us_release = -1;
foreach($releases['countries'] as $k=>$v) {
if(array_search('US', $v)) {
$us_release = $k;
break;
} else {
$us_release = null;
}
}
$uk_rating = $releases['countries'][$uk_release]['certification'];
$us_rating = $releases['countries'][$us_release]['certification'];
然后使用简单的<?php echo $uk_rating; ?>
/将其解析到页面中<?php echo $us_rating; ?>
。
但是,我发现某些数组实际上并不包含查询正在搜索的数据,例如以下没有英国评级或发布日期的数据:
Array
(
[id] => 13655
[countries] => Array
(
[0] => Array
(
[iso_3166_1] => US
[certification] => G
[release_date] => 2008-06-20
)
[1] => Array
(
[iso_3166_1] => CA
[certification] =>
[release_date] => 2008-06-20
)
[2] => Array
(
[iso_3166_1] => JP
[certification] =>
[release_date] => 2008-08-01
)
)
)
我试图构建各种 IF 语句来检查两个查询之一是否为空、空、== "" 等,但似乎没有任何效果。目前,我正在尝试以下操作,基本上使用逻辑如果两个结果都不等于无,则显示两者,或者如果两者之一为空,则仅显示不为空的那个(为格式化道歉):
if ($uk_date !== "" && $us_date !== "") {?>
<section class="span3">
<p class="overview-title">RELEASE DATE</p>
<p class="section_body">
<?php echo $uk_date; ?> (UK)<br>
<?php echo $us_date; ?> (US)
</p>
</section><?php
} else if ($uk_date = "") {?>
<section class="span3">
<p class="overview-title">RELEASE DATE</p>
<p class="section_body">
<?php echo $us_date; ?> (US)
</p>
</section><?php
} else if ($uk_date == "") {?>
<section class="span3">
<p class="overview-title">RELEASE DATE</p>
<p class="section_body">
<?php echo $uk_date; ?> (UK)
</p>
</section><?php
}