PHP 数组集和循环

PHP Array set and loop

我正在编写一个脚本,它将为您提供有关某些 Cisco 设备的信息。

class COMMAND
{
    ...
    private $intbrief;
    private $core;
    private $allcommand;
    ...

    public function __construct()
    {
        require_once 'class.php';
        $this->core = new CLASS();


        $this->allcommand = array(
        array(  "id"    => "brief",
                "def"   => "show ip interface brief | exclude un",
                "vrf"   => false,
                "val"   => "",
        ),
        array(  "id"    => "vrf",
                "def"   => "show ip interface ".$this->intbrief." | include VPN Routing/Forwarding",
                "vrf"   => false,
                "val"   => "",
        ));
    }

    ...

    private function validip($ip)
    {
        // return true or false
    }

    public function go($_ip, $_user, $_pass )
    {
        if($this->validip($_ip))
        {
            $this->ip = $_ip;
            $this->user = $_user;
            $this->pass = $_pass;

            return $this->logic();      
        } else {
            echo "Invalid Options\n";
            exit(1);
        }           
    }


    private function logic()
    {
        foreach ($this->allcommand as $i => &$row)
        {                               
            if(method_exists($this, $row['id']))
            {
                if(!empty($this->intbrief))
                {
                    echo "\n\n\n\t".$this->intbrief."---TRUE\n\n\n";
                }

                if(isset($this->vrf) and $row['vrf'] == true)
                {               
                    $command = $row['def']." vrf ".$this->vrf;
                } else {
                    $command = $row['def'];
                }

                // Send commands to telnet class
                $output = $this->core->GetOutputOf($this->ip, $this->user, $this->pass, $command);


                // Send to parse functions
                $row['val'] = $this->$row['id']($output);

            } else {
                echo "Check functions from allcommand array\n";
                exit(1);
            }   
        }
        return $this->allcommand;   
    }

    private function brief($match)
    {
        // parsing output ...
        $this->intbrief = $output; // e.g. $this->intbrief = "FastEthernet0/1";

    }

    private function vrf($match)
    {
        // parsing output ...
        $this->vrf = $output;
    }
}

$com = new COMMAND();
$f = $com->go($ip, $user, $password);

循环工作正常,但是...

数组 vrf def 是 show ip interface | include VPN Routing/Forwarding - 没有 $this->intbrief,因为它是空的....但为什么呢?我在循环中检查了它...

if(!empty($this->intbrief))
{
    echo "\n\n\n\t".$this->intbrief."---TRUE\n\n\n";
}

...这是真的。那么错误在哪里?

我想执行类似 show ip interface FastEthernet0/1 | include VPN ...

的操作

我会从私有功能简介

中获取信息"FastEthernet0/1"

感谢帮助

cdpb

我重新考虑问题并测试了一下,我认为问题出在数组创建上。创建数组时,解析数组值中的变量,然后将值固定在此数组实例中。所以我认为将 $this->allcommand 放在构造或任何其他函数中并不重要。为了解决这个问题而不是过多地改变 class 的结构,我建议在将数组 allcommand 声明为

$this->allcommand = array(
array(  "id"    => "brief",
        "def"   => "show ip interface brief | exclude un",
        "vrf"   => false,
        "val"   => "",
),
array(  "id"    => "vrf",
        "def"   => "show ip interface <inf> | include VPN Routing/Forwarding",
        "vrf"   => false,
        "val"   => "",
));

然后您可以在函数逻辑中,在获得 $command = $row['def']; 之后执行 preg_replacestr_replace<inf> 转换为 [=16 的值(如果有的话) =].可能不是最好的解决方案,但我可以提出来。

好主意!

我还进行了一些测试,并在一个单独的函数中构建了类似 while 循环的东西,它一次只给出数组的一部分。背后的想法是,在每一轮循环中,里面的变量都会被重新设置。

看起来像:

    ....

private function arraycommand()
{
    static $i = 1;
    $allcommand = array(
        array(  "id"    => "brief",
                "def"   => "show ip interface brief | exclude un",
                "vrf"   => false,
        ),
        array(  "id"    => "vrf",
                "def"   => "show ip interface ".$this->intbrief." | include VPN Routing/Forwarding",
                "vrf"   => false,
        ),
        );

    while(count($allcommand) >= $i)
    {
        $t = $i -1;
        $i++;
        return $allcommand[$t]; 
    }
    return false;
}

private function logic()
{
    while(true)
    {
        $allcommand = array($this->arraycommand());

        if(!$allcommand[0] == false)
        {
            foreach ($allcommand as $i => &$row)
            {
                ...
            }
        } else break;
    }
    return output
}

....

它的编码不是很漂亮,但它可以工作:)

感谢您的帮助!!!