简体   繁体   English

如何从PHP中的字符串获取数组值?

[英]How to get array values from the string in PHP?

I have a sting, In that string, my array values are stored. 我有一个问题,在该字符串中,存储了我的数组值。 How can I get array value from the string? 如何从字符串中获取数组值? Is there any predefined function in PHP or will achieve using REGEX? PHP中是否有任何预定义函数,或者将使用REGEX实现?

$rule ="array(
array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
)";

if I do print_r($rule); 如果我做print_r($ rule); it's giving string value only same as above. 它只提供与上面相同的字符串值。 But I want output like 但是我想要输出像

Array ( [0] => Array ( [parameter] => signature name [values] => RNSOC023 [condition] => EQUALS [rule type] => AND ) [1] => Array ( [parameter] => DestinationZone [values] => l3-internet [condition] => EQUALS [rule type] => AND ) [2] => Array ( [parameter] => Application [values] => ftp,sftp [condition] => EQUALS [rule type] => AND ) )

Finally got the solution, Try this: 终于得到了解决方案,试试这个:

$rule ="array(
    array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
    array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
    array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
    )";
    $returnValue = '';
    eval("\$returnValue = $rule;");
    print_r($returnValue);

eval() is EVIL! eval()是邪恶的! It renders your code open to all sorts of security issues and should be avoided at all costs. 它使您的代码容易受到各种安全问题的影响,因此不惜一切代价避免这样做。 Try this instead: 尝试以下方法:

$rule ="array(
array('parameter'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'),
array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND')
)";
preg_match_all("/(?:array\()('.*?'\s*=>\s*'.*?'\s*)+(?:\))/", $rule, $matches);
$rulearray = array();
$i = 0;
foreach ($matches[1] as $value) {
    preg_match_all("/('.*?'\s*=>\s*'.*?')/", $value, $m);
    foreach ($m[1] as $e) {
        list ($k, $v) = preg_split('/\s*=>\s*/', $e);
        $rulearray[$i][$k] = $v;
    }
    $i++;
}
print_r($rulearray);

Output: 输出:

Array ( [0] => Array ( ['parameter'] => 'signature name'
                       ['values'] => 'RNSOC023'
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' ) 
        [1] => Array ( ['parameter'] => 'DestinationZone' 
                       ['values'] => 'l3-internet' 
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' ) 
        [2] => Array ( ['parameter'] => 'Application'
                       ['values'] => 'ftp,sftp'
                       ['condition'] => 'EQUALS'
                       ['rule type'] => 'AND' )
)

The code will be executed in the scope of the code calling eval(). 该代码将在调用eval()的代码范围内执行。 Thus any variables defined or changed in the eval() call will remain visible after it terminates. 因此,在eval()调用中定义或更改的任何变量在终止后将保持可见。 For More Info HERE 欲了解更多信息,请点击这里

eval("\$rule = $rule;");
print_r($rule);

The approach you are using to keep array of data in a database is wrong, You should use json string format in the database and when getting data, you can json_decode function in php to convert into array. 您用于在数据库中保留数据数组的方法是错误的,应该在数据库中使用json字符串格式,并且在获取数据时,可以在php中使用json_decode函数将其转换为数组。 Try to convert that array into json string and store in the db. 尝试将该数组转换为json字符串并存储在db中。 It's the best practise. 这是最好的做法。 That's why you are getting these types of issues. 这就是为什么您遇到这些类型的问题。

So basically you would want json_encode() to make the array. 因此,基本上,您希望json_encode()创建数组。 What I tested now was the following: 我现在测试的内容如下:

$rule = array(array('parameter1'=> 'signature name', 'values'=>'RNSOC023', 'condition'=>'EQUALS', 'rule type'=>'AND'), 1 => array('parameter2'=> 'DestinationZone', 'values'=>'l3-internet', 'condition'=>'EQUALS', 'rule type'=>'AND'), 2 => array('parameter'=> 'Application', 'values'=>'ftp,sftp', 'condition'=>'EQUALS', 'rule type'=>'AND'));
$jr = json_encode($rule);
$jd = json_decode($jr, true);
print $jd[0]['values'];

The above worked, and tested on my environment. 以上工作,并在我的环境上进行了测试。

For more information on json_encode(); 有关json_encode();更多信息json_encode(); and json_decode(); json_decode();

If you run print_r($jd); 如果运行print_r($jd); you will get an output of: 您将获得以下输出:

Array ( [0] => Array ( [parameter1] => signature name [values] => RNSOC023 [condition] => EQUALS [rule type] => AND ) [1] => Array ( [parameter2] => DestinationZone [values] => l3-internet [condition] => EQUALS [rule type] => AND ) [2] => Array ( [parameter] => Application [values] => ftp,sftp [condition] => EQUALS [rule type] => AND ) ) 

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

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