if /else 内的 foreach

发布于 2024-11-27 14:26:58 字数 537 浏览 1 评论 0原文

目前我有这个

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你丑哭了我 2024-12-04 14:26:58

这是一个快速简单的解决方案:

$addrfound=false;
foreach ($data->address as $address) {
  if ($address->fullAddress) { echo $address->fullAddress; $addrfound=true; }
  if ($address->phones->PhoneNumber) { echo $address->phones->PhoneNumber); $addrfound=true; } 
}

if (!$addrfound)
    if ($data->relatedPhone) echo $data->relatedPhone;

here is a quick simple solution:

$addrfound=false;
foreach ($data->address as $address) {
  if ($address->fullAddress) { echo $address->fullAddress; $addrfound=true; }
  if ($address->phones->PhoneNumber) { echo $address->phones->PhoneNumber); $addrfound=true; } 
}

if (!$addrfound)
    if ($data->relatedPhone) echo $data->relatedPhone;
凉城 2024-12-04 14:26:58

您可以执行 else if 而不是另一个 if

if (foo) {
  echo "bar";
} else if (foo2) {
  echo "bar2";
} else {
  echo "foobar";
}

仅当所有其他 if 语句时才运行最后一个 else评估为假。

所以对于你的情况,我会尝试这个:

// display all address with their associated phone numbers
    foreach ($data->address as $address) {
      if ($address->fullAddress) {
        echo $address->fullAddress;
      } else if ($address->phones->PhoneNumber) {
        echo $address->phones->PhoneNumber;
      } else if ($data->relatedPhone) {
        echo $data->relatedPhone;
      }
    }

You can do an else if instead of another if:

if (foo) {
  echo "bar";
} else if (foo2) {
  echo "bar2";
} else {
  echo "foobar";
}

The last else is run only if all other if statements evaluate to false.

So in your case, I'd try this:

// display all address with their associated phone numbers
    foreach ($data->address as $address) {
      if ($address->fullAddress) {
        echo $address->fullAddress;
      } else if ($address->phones->PhoneNumber) {
        echo $address->phones->PhoneNumber;
      } else if ($data->relatedPhone) {
        echo $data->relatedPhone;
      }
    }
北城半夏 2024-12-04 14:26:58

您需要将逻辑构建为更容易处理的 if 语句:

// display all address with their associated phone numbers
foreach ($data->address as $address) 
{
    $hasAddress = (bool) $address->fullAddress;
    $hasPhone = (bool) $address->phones->PhoneNumber;
    $isAddress = ??; # add your decision here, I can't see you specified it in your question.

    if ($hasAddress) echo $address->fullAddress;
    if ($hasPhone) echo $address->phones->PhoneNumber;

    // display all phone numbers if there is not an address
    if (!$isAddress) {
        if ($data->relatedPhone) echo $data->relatedPhone;
    }
}

You need to structure your logic into if statements that are more easy to deal for you:

// display all address with their associated phone numbers
foreach ($data->address as $address) 
{
    $hasAddress = (bool) $address->fullAddress;
    $hasPhone = (bool) $address->phones->PhoneNumber;
    $isAddress = ??; # add your decision here, I can't see you specified it in your question.

    if ($hasAddress) echo $address->fullAddress;
    if ($hasPhone) echo $address->phones->PhoneNumber;

    // display all phone numbers if there is not an address
    if (!$isAddress) {
        if ($data->relatedPhone) echo $data->relatedPhone;
    }
}
2024-12-04 14:26:58

您可以创建一个标志来指示是否至少满足其中一个条件。

$flag = 0;
if (condition_1) {
  echo $output_1;
  $flag = 1;
}

if (condition_2) {
  echo $output_2;
  $flag = 1;
}

if (!$flag && condition_3) {
  echo $output_3;
}

You could create a flag to signal whether at least one of the conditions is met.

$flag = 0;
if (condition_1) {
  echo $output_1;
  $flag = 1;
}

if (condition_2) {
  echo $output_2;
  $flag = 1;
}

if (!$flag && condition_3) {
  echo $output_3;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文