简体   繁体   English

将数据存储在数组中并搜索项目

[英]Store data in a array and search for items

I Connect to a remote SSH device in Php . 我连接到Phpremote SSH设备。 I run a command that output is too long. 我运行的command输出太长。 So I searched and understood I can save it via : 所以我搜索并了解可以通过以下方式保存它:

   $my_array= explode("\n", $this->ssh->exec('some command'));

Its ok, Now I can display whole output via : 好的,现在我可以通过显示整个输出:

echo print_r($my_array);

Output is something like : 输出是这样的:

Array ( [0] => ath4_auth_mode=disabled [1] => wl_mac_deny= [2] => filter_dport_grp3= [3] => ses_script= [4] => http_redirect_port=3128 [5] => oet5_en=0 [6] => filter_dport_grp4= [7] => oet2_fragment=0 [8] =>

When I run : 当我跑步时:

echo count($my_array);

It displays me : 它显示了我:

2200

So it is true, Now I wanna search for a specific text like name= ,I want the value after equal in the array, I tried this : 所以这是真的,现在我想搜索诸如name=的特定文本,我想要在数组中等于之后的值,我尝试了以下操作:

           $search_result = array_search("name=", $my_array);

But no chance, Even tried : 但是没有机会,甚至尝试过:

foreach($my_array as $cat) {
$cat = trim($cat);
if($cat == "name=") { 
    echo "hoola !";
} else { 
    echo ':-(';
}

Again no chance, How can I search for the name= and get the value after = ? 再次没有机会,我该如何搜索name=并获取=之后的值?

I suppose in your array value is not name= exactly. 我想在您的数组值不是name=确切。 It can be name=something for example. 例如,它可以是name=something In this case neither == nor in_array will find it. 在这种情况下, ==in_array都找不到。 You need to use strpos : 您需要使用strpos

foreach($my_array as $cat) {
    $cat = trim($cat);
    if(strpos($cat, "name=") === 0) { 
        echo "hoola !", $cat;

        // add a break if value found so as not to search other elements
        break;
    } else { 
        echo ':-(';
    }
}

I used === and 0 because name= must be in the beginning of your $cat . 我使用了===0因为name=必须在$cat的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM