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

时间:2013-03-22 14:40:51

标签: php arrays

假设我有一个数字,让我们说5.现在让我们假设有5个成员。现在每个成员开始计数1到2.那些获得第二个号码的成员离开然后再次计数从下一个成员开始。所以最后在这种情况下,第三个成员停留在最后。

所以我试着像这样实现。首先将成员分配为数组$ 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);

输出

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

现在我想计算从键5(第5个成员)再到1个(第1个成员)的数字,依此类推。

所以最后一个键3(第3个成员)离开。
我想打印剩下的最后一个成员。

我怎样才能实现这个目标?

我无法理解然后看看这个 Survival Strategy

5 个答案:

答案 0 :(得分:1)

这将删除数组中的所有其他项,直到只剩下一个项目为止。

$members = range(1, 5);

$i = 0;

while(count($members) > 1) {
 $i++;
 if($i == count($members)) $i = 0;
 unset($members[$i]);
 $members = array_values($members);
 if($i == count($members)) $i = 0;
}

echo $members[0];

答案 1 :(得分:1)

这是一个面向对象的解决方案,具有易于遵循的简化方法和多个示例。

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.\n";
}

答案 2 :(得分:0)

嗯,我可以推荐两个功能:

  1. http://php.net/manual/en/function.array-keys.php

    这将重新索引您的数组,索引为:0,1,2

  2. http://php.net/manual/en/control-structures.foreach.php

    有了这个,您可以通过以下方式浏览任何数组:

    foreach($ v1 as $ key =&gt; $ value){...选择最大值等...}

答案 3 :(得分:0)

<?php

function build_thieves($thieves)
{
    return range(1, $thieves);
}

function kill_thief(&$cave)
{
    if(sizeof($cave)==1)
    {
        $thief=array_slice($cave, 0, 1);
        echo $thief.' survived';
        return false;
    }

    $thief=array_slice($cave, 0, 1);
    array_push($cave, $thief);

    $thief=array_slice($cave, 0, 1);
    echo $thief.' killed';
    return true;
}

$cave=build_thieves(5);
$got_data=true;
while($got_data)
{
    $got_data=kill_thief($cave);
}

调整到每2,而不是每3。从1开始不是0

答案 4 :(得分:0)

这个答案有点复杂,但效率更高。它不会创建项目数组,然后将其删除。它以一个值(例如1)开始,并计算尚未被删除的下一个项目。然后,它将其标记为已删除。如果您实际拥有一系列项目,则最终项目的索引将为$ current - 1.以下示例使用值1到10,000。在我的机器上,它只需要0.05秒。

define('SIZE', 10000);

/**
 * Helper function to return the next value
 */
function get_next($current, &$removed) {
  $next = $current;
  do {
    $next++;
  } while(isset($removed[$next]));

  return ($next > SIZE) ? get_next(0, $removed) : $next;
}

$current   = 1;
$removed   = array();
$remaining = SIZE;

$start_time = microtime(true);

while($remaining > 1) {
  $current           = get_next($current, $removed);
  $removed[$current] = 1;
  $remaining         = SIZE - count($removed);
  $current           = get_next($current, $removed);
}

$total_time = microtime(true) - $start_time;

echo "Processed " . SIZE . " items\n";
echo "Winning item: {$current}\n";
echo "Total time: {$total_time} seconds\n";
相关问题