12

我想用 0 替换所有数组值,除了workand home

输入:

$array = ['work', 'homework', 'home', 'sky', 'door']

我的编码尝试:

$a = str_replace("work", "0", $array);

预期输出:

['work', 0, 'home', 0, 0]

此外,我的输入数据来自用户提交,数组元素的数量可能非常大。

4

10 回答 10

15

更优雅和更短的解决方案。

$aArray = array('work','home','sky','door');   

foreach($aArray as &$sValue)
{
     if ( $sValue!='work' && $sValue!='home' ) $sValue=0;
}

& 运算符是指向数组中特定原始字符串的指针。(而不是该字符串的副本)您可以通过这种方式为数组中的字符串分配一个新值。你唯一不能做的是任何可能扰乱数组顺序的事情,比如 unset() 或键操作。

上面示例的结果数组将是

$aArray = array('work','home', 0, 0)
于 2012-11-13T18:30:04.720 回答
11

一个循环会多次执行一系列动作。因此,对于数组中的每个元素,您将检查它是否等于您要更改的元素,如果是,请更改它。还要确保在字符串周围加上引号

//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/en/language.types.array.php

http://www.php.net/manual/en/language.control-structures.php

但是,如果您在诸如循环之类的东西上苦苦挣扎,您可能需要阅读一些介绍性的编程材料。这应该可以帮助您真正了解正在发生的事情。

于 2011-03-03T11:12:32.047 回答
1

其他更快的方法,但确实需要一个循环:

//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
)
于 2012-12-23T19:14:24.293 回答
1

使用 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);
于 2014-03-09T17:59:18.343 回答
1

你可以试试 array_walk 函数:

function zeros(&$value) 
{ 
    if ($value != 'home' && $value != 'work'){$value = 0;}      
}   

 $asting = array('work','home','sky','door','march');   

 array_walk($asting, 'zeros');
 print_r($asting);
于 2014-06-19T08:28:20.217 回答
0

您还可以在 str_replace 上将数组作为参数 1 和 2...

于 2011-03-03T11:31:54.423 回答
0

只是for循环的一小部分。许多人没有意识到每次新的迭代都会完成第二个比较任务。因此,如果是大数组或计算的情况,您可以通过以下方式优化循环:

for ($i = 0, $c = count($asting); $i < $c; $i++) {...}

您可能还想查看http://php.net/manual/en/function.array-replace.php的原始问题,除非代码确实是最终的 :)

于 2011-03-04T22:30:04.377 回答
0

试试这个

$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);
于 2018-06-30T20:02:38.820 回答
0

这个页面上有一些技术可以实现零迭代函数调用——这在性能方面很好。为了获得最佳的可维护性,我建议将您的目标字符串列表分隔为查找数组。通过引用修改原始数组值,您可以快速替换整个字符串并将非目标值合并为 0。

代码:(演示

$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)
);
于 2022-02-07T12:16:21.363 回答
-1

这是我的最终代码

//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;
于 2011-03-04T02:14:39.633 回答