我试过这个脚本,结果是真的
$a = array('Allow','Block');
$b = array('yes','no');
$c=array_combine($a,$b);
while (list($key, $value) = each($c)) {
echo "$key: $value \n";
}
真实的结果:
允许:是阻止:否
但是当我在数组中使用变量
时$val1 = "'Allow','Block'";
$val2 = "'yes','no'";
$a = array($val1);
$b = array($val2);
结果不同:
'允许''阻止':'是',' no'
问题是如何制作第一个结果,因为我从动态变量
获得了文本答案 0 :(得分:2)
您不能像第二个示例中那样使用数组。你应该尝试这样的事情:
$val1 = "Allow,Block";
$val2 = "yes,no";
$a = explode(',',$val1);
$b = explode(',', $val2);
答案 1 :(得分:1)
请尝试以下代码。
$val1 = "Allow,Block";
$val2 = "yes,no";
$a = explode(',',$val1);
$b = explode(',', $val2);
$c=array_combine($a,$b);
while (list($key, $value) = each($c)) {
echo "$key: $value \n";
}
答案 2 :(得分:1)
您还可以使用简单的foreach来获得真实的结果:
$a = array('Allow','Block');
$b = array('yes','no');
$trueResult = array();
foreach($a as $key => $value){
$trueResult[$value] = $b[$key];
}
foreach($trueResult as $key => $val){
echo $key . "=". $val."<br>";
}
答案 3 :(得分:1)
$val1 = "'Allow','Block'";
$val2 = "'yes','no'";
$val1 = str_replace("'", "", $val1);
$val2 = str_replace("'", "", $val2);
$c = array_combine(explode(',', $val1), explode(',', $val2));
while (list($key, $value) = each($c)) {
echo "$key: $value \n";
}