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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?