开发者

If foreach value appear, add some data

I got:

foreach ($query as $sample) 开发者_如何学Go{
[..]
}

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜