我想将数组中除了work
和home
之外的所有值替换为0。
输入:
$array = ['work', 'homework', 'home', 'sky', 'door']
我的编程尝试:
$a = str_replace("work", "0", $array);
期望输出结果:
['work', 0, 'home', 0, 0]
另外,我的输入数据来自用户提交,数组元素的数量可能非常大。
更加优雅而且简洁的解决方案。
$aArray = array('work','home','sky','door');
foreach($aArray as &$sValue)
{
if ( $sValue!='work' && $sValue!='home' ) $sValue=0;
}
&运算符是指向数组中特定原始字符串的指针。(而不是该字符串的副本)这样,您可以将新值赋给数组中的字符串。您不能做的唯一一件事是任何可能干扰数组顺序的事情,比如unset()或键操作。
上面示例的结果数组将是:
$aArray = array('work','home', 0, 0)
//Setup the array of string
$asting = array('work','home','sky','door')
/**
Loop over the array of strings with a counter $i,
Continue doing this until it hits the last element in the array
which will be at count($asting)
*/
for($i = 0; $i < count($asting);$i++){
//Check if the value at the 'ith' element in the array is the one you want to change
//if it is, set the ith element to 0
if ($asting[$i] == 'work' || $asting[$i] == 'home')
$asting[$i] = 0;
}
这里有一些建议阅读材料:
http://www.php.net/manual/zh/language.types.array.php
http://www.php.net/manual/zh/language.control-structures.php
但是如果你在循环等方面遇到困难,可能需要阅读一些入门编程材料。这将帮助你真正理解发生了什么。
//Setup the array of string
$asting = array('bar', 'market', 'work', 'home', 'sky', 'door');
//Setup the array of replacings
$replace = array('home', 'work');
//Loop them through str_replace() replacing with 0 or any other value...
foreach ($replace as $val) $asting = str_replace($val, 0, $asting);
//See what results brings:
print_r ($asting);
将输出:
Array
(
[0] => bar
[1] => market
[2] => 0
[3] => 0
[4] => sky
[5] => door
)
function zeros(&$value)
{
if ($value != 'home' && $value != 'work'){$value = 0;}
}
$asting = array('work','home','sky','door','march');
array_walk($asting, 'zeros');
print_r($asting);
使用array_map的另一种方法:
$original = array('work','home','sky','door');
$mapped = array_map(function($i){
$exclude = array('work','home');
return in_array($i, $exclude) ? 0 : $i;
}, $original);
$array = ['work', 'homework', 'home', 'sky', 'door'];
$keep = ['work', 'home'];
$lookup = array_combine($keep, $keep);
foreach ($array as &$v) {
$v = $lookup[$v] ?? 0;
}
var_export($array);
输出:
array (
0 => 'work',
1 => 0,
2 => 'home',
3 => 0,
4 => 0,
)
你可以通过简单地扩展$keep
来轻松、干净地扩展你的目标字符串列表。
如果您不想使用传统的循环,可以使用相同的技术而不修改原始数组。(演示)
var_export(
array_map(fn($v) => $lookup[$v] ?? 0, $array)
);
试一下
$your_array = array('work','home','sky','door');
$rep = array('home', 'work');
foreach($rep as $key=>$val){
$key = array_search($val, $your_array);
$your_array[$key] = 0;
}
print_r($your_array);
foreach()
中声明$key
,然后立即在下一行上覆盖它?如果$val
在$your_array
中找不到怎么办?... array_search()
将返回false
,并且false
将转换为0
并替换$your_array
中的第一个元素,无论其值如何。我认为更准确的说法是:“不要尝试这个”。 - mickmackusa关于for循环,有一个小细节需要注意。许多人没有意识到第二个比较任务在每次新迭代中都会执行。因此,如果涉及大数组或计算,您可以通过执行以下操作来优化循环:
for ($i = 0, $c = count($asting); $i < $c; $i++) {...}
除非代码真的是最终版本,否则您可能还想查看http://php.net/manual/en/function.array-replace.php以解决原始问题 :)
你也可以将数组作为参数1和2传递给str_replace函数...
这是我的最终代码
//Setup the array of string
$asting = array('work','home','sky','door','march');
/**
Loop over the array of strings with a counter $i,
Continue doing this until it hits the last element in the array
which will be at count($asting)
*/
for($i = 0; $i < count($asting); $i++) {
//Check if the value at the 'ith' element in the array is the one you want to change
//if it is, set the ith element to 0
if ($asting[$i] == 'work') {
$asting[$i] = 20;
} elseif($asting[$i] == 'home'){
$asting[$i] = 30;
}else{
$asting[$i] = 0;
}
echo $asting[$i]."<br><br>";
$total += $asting[$i];
}
echo $total;
$asting[$i] = 0;
。 - alexn