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 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染