• 欢迎访问IT乐园(o゚▽゚)o
  • 推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。

php获取相邻下标

php fhy 7年前 (2017-04-01) 4973次浏览 0个评论

该类用于获取 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;
    }

 


IT 乐园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 获取相邻下标
喜欢 (5)
关于作者:
九零后挨踢男
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址