douqilin4296 2013-03-22 14:40
浏览 72
已采纳

如何在php中循环迭代数组循环?

Suppose i have a number let's say 5. Now lets assume that there are 5 members. Now each member started to count 1 to 2. Those member who get 2nd number leaves and then again count start from the next member. so at last in this scenario the 3rd member stays at last.

So i tried to implement like this. First of assign members as array $v.

for($i=1 ; $i<=5 ; $i++)
{
    $v[] = $i;
}

$v1 = array_flip($v);

for($i=0 ; $i<=5 ; $i += 2 )
{   
    unset($v1[$i]);
}

echo "<pre>";
print_r($v1);

output

Array
(
    [1] => 0
    [3] => 2
    [5] => 4
)

Now i want to count the numbers from key 5(5th member) to again 1(1st member) and so on.

so at last key 3(3rd member) left.
I want to print the last member that left.

How can i achieve this?

I you can't understand then look at this Survival Strategy

  • 写回答

5条回答 默认 最新

  • dsmvovm27249 2013-03-25 20:01
    关注

    Here's an Object-Oriented solution with an easy-to-follow reduce method and multiple examples.

    class CountByTwoArrayReducer {
    
      public function __construct($array) {
        $this->array = $array;
        $this->size  = count($array);
      }
    
      public function reduce() {
        $this->initialize();
    
        while($this->hasMultipleItems()) {
          $this->next();
          $this->removeCurrentItem();
          $this->next();
        }
    
        return $this->finalItem();
      }
    
      protected function initialize() {
        $this->current   = 1;
        $this->removed   = array();
        $this->remaining = $this->size;
      }
    
      protected function hasMultipleItems() {
        return ($this->remaining > 1);
      }
    
      protected function next($start = null) {
        $next = ($start === null) ? $this->current : $start;
    
        do {
          $next++;
        } while(isset($this->removed[$next]));
    
        if($next > $this->size)
          $this->next(0);
        else
          $this->current = $next;
      }
    
      protected function removeCurrentItem() {
        $this->removed[$this->current] = 1;
        $this->remaining--;
      }
    
      protected function finalItem() {
        return $this->array[$this->current - 1];
      }
    
    }
    
    $examples = array(
      array('A', 'B', 'C', 'D', 'E'),
      range(1, 100),
      range(1, 1000),
      range(1, 10000)
    );
    
    foreach($examples as $example) {
      $start = microtime(true);
    
      $reducer = new CountByTwoArrayReducer($example);
      $result  = $reducer->reduce();
    
      $time = microtime(true) - $start;
    
      echo "Found {$result} in {$time} seconds.
    ";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了