如果出现foreach值,则添加一些数据

发布于 2024-12-08 18:14:46 字数 886 浏览 0 评论 0原文

我得到:

foreach ($query as $sample) {
[..]
}

并且我需要更改它,因此如果所有带有

$sample['key'] == 1

的值都会被 foreach 循环,并且下一个循环将具有 $sample[ 'key'] == 0 ,然后它会添加例如:

<tr><td colspan="4">All keys with $sample['key'] == 1 are after the loop, and I'm staring to loop with $sample['key'] == 0</td></tr>

但仅一次。

//编辑

试图解释更多:

首先:foreach将循环这个:

foreach($query as $sample) {
*loop*
print_r($sample['key']) //1
*loop*
print_r($sample['key']) //1
...etc.
}

但是如果出现类似的内容:

foreach($query as $sample) {
*loop*
print_r($sample['key']) //1
*loop*
*adding some content, because next print value is 0!*
print_r($sample['key']) //0!!!!!!!
}

希望你现在明白了,我已经尽力解释了能。这很难描述,所以如果您有任何疑问,请随时在评论中提问。

I got:

foreach ($query as $sample) {
[..]
}

And I need it to be changed so if all values with

$sample['key'] == 1

Gonna be looped by the foreach and the next loop will have $sample['key'] == 0 , then it'll add for example something like:

<tr><td colspan="4">All keys with $sample['key'] == 1 are after the loop, and I'm staring to loop with $sample['key'] == 0</td></tr>

But only once.

//edited

Trying to explain more:

At first: foreach will loop this:

foreach($query as $sample) {
*loop*
print_r($sample['key']) //1
*loop*
print_r($sample['key']) //1
...etc.
}

But if there'll be something like:

foreach($query as $sample) {
*loop*
print_r($sample['key']) //1
*loop*
*adding some content, because next print value is 0!*
print_r($sample['key']) //0!!!!!!!
}

Hope you understand it now, I've done my best to explain as best as I can. It's hard to describe, so if you have some questions, feel free to ask in the comments.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柒七 2024-12-15 18:14:46

我不确定我 100% 理解这个问题,但听起来几乎像 array_filter 可能会有一些帮助:

function KeyIsEqualToOne($ary){
  return $ary['key'] == 1;
}

function KeyIsEqualToZero($ary){
  return $ary['key'] == 0;
}


// all elements where key==1
$KeysWithOne = array_filter($query, 'KeyIsEqualToOne');

// all elements where key==0
$KeysWithZero = array_filter($query, 'KeyIsEqualToZero');

否则你总是可以保留一个状态变量来查看何时进行切换:

$HasSeenZeroValue = false;
foreach ($query as $sample){
  // ...
  if ($sample['key'] == 0 && !$HasSeenZeroValue){
    echo '<tr><td>...</td></tr>';
    $HasSeenZeroValue = true;
  }
}

不过,不可否认,我不知道100% 得到你想要的询问。

I'm not sure I 100% understand the question, but it sounds almost like array_filter may be of some help:

function KeyIsEqualToOne($ary){
  return $ary['key'] == 1;
}

function KeyIsEqualToZero($ary){
  return $ary['key'] == 0;
}


// all elements where key==1
$KeysWithOne = array_filter($query, 'KeyIsEqualToOne');

// all elements where key==0
$KeysWithZero = array_filter($query, 'KeyIsEqualToZero');

Otherwise you could always keep a state variable to see when the switch was made:

$HasSeenZeroValue = false;
foreach ($query as $sample){
  // ...
  if ($sample['key'] == 0 && !$HasSeenZeroValue){
    echo '<tr><td>...</td></tr>';
    $HasSeenZeroValue = true;
  }
}

Though, admittedly, I don't 100% get what you're asking.

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