if /else 内的 foreach
目前我有这个
// display all address with their associated phone numbers
foreach ($data->address as $address) {
if ($address->fullAddress) echo $address->fullAddress;
if ($address->phones->PhoneNumber) echo $address->phones->PhoneNumber);
}
,但如果不满足上述子句中的语句,我需要添加这个作为替代方案
// display all phone numbers if there is not an address
if ($data->relatedPhone) echo $data->relatedPhone;
如果没有满足任何子句,我根本不希望显示任何内容。那么我怎样才能将两者结合起来呢?
Currently I have this
// display all address with their associated phone numbers
foreach ($data->address as $address) {
if ($address->fullAddress) echo $address->fullAddress;
if ($address->phones->PhoneNumber) echo $address->phones->PhoneNumber);
}
but I need to add this as an alternative if the statements in the above clause are not met
// display all phone numbers if there is not an address
if ($data->relatedPhone) echo $data->relatedPhone;
If none of the clauses are met I want nothing to display at all. So how can I combine the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个快速简单的解决方案:
here is a quick simple solution:
您可以执行
else if
而不是另一个if
:仅当所有其他
if
语句时才运行最后一个else
评估为假。所以对于你的情况,我会尝试这个:
You can do an
else if
instead of anotherif
:The last
else
is run only if all otherif
statements evaluate to false.So in your case, I'd try this:
您需要将逻辑构建为更容易处理的 if 语句:
You need to structure your logic into if statements that are more easy to deal for you:
您可以创建一个标志来指示是否至少满足其中一个条件。
You could create a flag to signal whether at least one of the conditions is met.