1

我正在尝试遍历一个数组并替换不同数组的 in_array 值。

$array = array("username"=>"Bill", "email" => "Email Address");
$unset = array("Username","Email Address"); // Array of default values for inputs
foreach($array as $key => $value) {
    global $unset;
    if(in_array($value, $unset)) {
        $value = "-";
    }
}
print_r($array["email"]);

我想将 $array 中与默认值匹配的值替换为“-”。不希望取消设置和array_splice,保留空间。

4

2 回答 2

2

尝试这个:

$array = array("username"=>"Bill", "email" => "Email Address");
$unset = array("Username","Email Address"); // Array of default values for inputs

foreach($array as &$value) {
    if(in_array($value, $unset)) {
        $value = "-";
    }
}
print_r($array["email"]);
于 2012-08-30T01:31:32.010 回答
0

另一种方法是

$array = array("username"=>"Bill", "email" => "Email Address");
$unset = array("Username","Email Address"); // Array of default values for inputs
foreach($array as $key => $value) {
    if(in_array($value, $unset)) {
        $array[$key] = "-";
    }
}
print_r($array["email"]);
于 2012-08-30T01:37:11.333 回答