有没有办法在PHP重新启动一个循环在foreach,或更改测试值在交换机? [英] is there a way in PHP to restart a loop in a foreach, or change the test value in a switch?

查看:174
本文介绍了有没有办法在PHP重新启动一个循环在foreach,或更改测试值在交换机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在循环一个数组,并且在其中一个循环的中间,我发现了一些小问题,请更改...某事...,并且需要重试...有没有办法跳回到循环的顶部,而不抓取数组中的下一个值?

我怀疑这存在,但它会是一些关键字像 continue break 。事实上,它会很像 continue ,除了它没有得到下一个项目,它保留了它在内存中的内容。

如果什么都不存在,我可以在数组中插入一些东西,它会成为循环中的下一个键/值?

也许这将是更容易一阵子(array_shift())...

或者我想循环内部的递归函数可能工作。



好吧,我的问题正在演变,因为我输入这个,所以请查看这个伪代码:

  foreach($ storage_locations as $ storage_location){
switch($ storage_location){$ b $ case'cookie':
if(headers_sent()){
//不能存储在cookie中,故障转移到会话
//我能在这里做什么来运行下一个案例中的代码?
//追加'会话'到$ storage_locations?
//这将使它运行,但数组中的其他项目将首先运行...我怎么能得到它下一个?
} else {
set_cookie();
return;
}
break;

case'session':
set_session();
return;
break;


$ / code $ / pre

我确定没有关键字可以改变值在交换机中间测试...所以我应该重构这个代码来获得我的故障转移?

解决方案

不用 foreach ,但是用更多的手动数组迭代:

  while( list($ key,$ value)= each($ array)){
if(...){
reset($ array); //再次启动


code $ <$ $ $ $ $ $ $ $ $ $ $ < //php.net/each\">http://php.net/each

http:// php .net / reset



看起来像是一个简单的下降将做的伎俩,虽然:



<$如果(!headers_sent()){
set_cookie();
case'cookie':
> $ code> switch($ storage_location)
break;
}

//转入下一个案例

案例'session':


if i'm looping over an array, and while in the middle of one of the loops i discover some small issue, change ...something..., and need to try again ... is there a way to jump back to the top of the loop without grabbing the next value out of the array?

i doubt this exists, but it would be some keyword like continue or break. in fact, it would be a lot like continue, except that it doesn't get the next item, it maintains what it has in memory.

if nothing exists, can i insert something into the array in such a way that it will become the next key/value in the loop?

maybe this would be easier with a while(array_shift())...

or i suppose a recursive function inside the loop might work.

well, my question is evolving as i type this, so please review this pseudo code:

foreach($storage_locations as $storage_location) {
    switch($storage_location){
        case 'cookie':
            if(headers_sent()) {
                // cannot store in cookie, failover to session
                // what can i do here to run the code in the next case?
                // append 'session' to $storage_locations?
                // that would make it run, but other items in the array would run first... how can i get it next?
            } else {
                set_cookie();
                return;
            }
        break;

        case 'session':
            set_session();
            return;
        break;
    }
}

i'm sure there is no keyword to change the value tested against in the switch mid-stream... so how should i refactor this code to get my failover?

解决方案

Not with a foreach, but with more manual array iteration:

while (list($key, $value) = each($array)) {
    if (...) {
        reset($array); // start again
    }
}

http://php.net/each
http://php.net/reset

It seems like a simple fall through would do the trick though:

switch ($storage_location) {
    case 'cookie':
        if (!headers_sent()) {
            set_cookie();
            break;
        }

        // falls through to next case

    case 'session':

这篇关于有没有办法在PHP重新启动一个循环在foreach,或更改测试值在交换机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆