该类用于获取 php 数组下标,指定一个下标获取相邻下标。
转自: http://blog.csdn.net/phpfenghuo/article/details/30991029
<?php class Steps { private $all; private $count; private $curr; function __construct() { $this->count = 0; } function add($step) { $this->count++; $this->all[$this->count] = $step; } function setCurrent($step) { reset($this->all); for ($i = 1; $i <= $this->count; $i++) { if ($this->all[$i] == $step) break; next($this->all); } $this->curr = current($this->all); } function getCurrent() { return $this->curr; } function getNext() { self::setCurrent($this->curr); return next($this->all); } function getPrev() { self::setCurrent($this->curr); return prev($this->all); } } $xoops[1] = '这'; $xoops[2] = '是'; $xoops[3] = '我'; $xoops[4] = '的'; $xoops[5] = '博'; $xoops[6] = '客'; $steps = new Steps(); foreach($xoops as $key=>$value){ $steps->add($key); } # 设置当前下标为 3 $steps->setCurrent(3); # 参数为 key 值 # 获取下标 echo '上一个下标:'.$steps->getPrev()."\n"; echo '指定的下标:'.$steps->getCurrent()."\n"; echo '下一个下标:'.$steps->getNext()."\n";
$db_node_template = json_decode($db_node_template, true); # 引入获取下标类 App::import('Vendor','Steps',array('file' => 'Steps.php')); $s = new Steps(); foreach ($db_node_template as $k => $v) { $s->add($k); if($k == $node){ $db_node_template[ $k ]['actual_date'] = $actual_date; $db_node_template[ $k ]['done'] = 1; } } # 设置当前的下标 $s->setCurrent($node); # 判断上一个节点是否完成 if( $s->getPrev() ){ if( $db_node_template[ $s->getPrev() ]['done'] != 1){ $this->_return_json_inf(400,'上一节点未完成!'); } } # 获取相邻下一个下标 if($s->getNext()){ # 设置下一个为进行中 $db_node_template[ $s->getNext() ]['done'] = 2; }