php - 替换数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5179606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php - replace array value
提问by wyman
want to replace the array value with 0, exclude work and home
想用 0 替换数组值,排除工作和家庭
array(work,home,sky,door);
I want use php to replace this array value,
我想用 php 替换这个数组值,
I have try to use this function to replace this array
我尝试用这个函数来替换这个数组
$asting = array(work,home,sky,door);
$a = str_replace("work","0",$asting);
how about my array is infinity to add from the form submitted, but I want to replace the value to 0 exclude the work & home only?
我的数组如何从提交的表单中添加无穷大,但我想将值替换为 0 只排除工作和家庭?
采纳答案by wyman
this my final code
这是我的最终代码
//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;
回答by Jonno_FTW
A loop will perform a series of actions many times. So, for each element in your array, you would check if it is equal to the one you want to change and if it is, change it. Also be sure to put quote marks around your strings
一个循环会多次执行一系列动作。因此,对于数组中的每个元素,您将检查它是否等于要更改的元素,如果是,则更改它。还要确保在字符串周围加上引号
//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;
}
Here is some suggested reading:
以下是一些建议阅读:
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.types.array.php
http://www.php.net/manual/en/language.control-structures.php
http://www.php.net/manual/en/language.control-structures.php
But if you are struggling on stuff such as looping, you may want to read some introductory programming material. Which should help you really understand what's going on.
但是如果你在循环之类的东西上苦苦挣扎,你可能需要阅读一些介绍性的编程材料。这应该可以帮助您真正了解正在发生的事情。
回答by Deckard
A bit more elegant and shorter solution.
更优雅和更短的解决方案。
$aArray = array('work','home','sky','door');
foreach($aArray as &$sValue)
{
if ( $sValue!='work' && $sValue!='home' ) $sValue=0;
}
The & operator is a pointer to the particular original string in the array. (instead of a copy of that string) You can that way assign a new value to the string in the array. The only thing you may not do is anything that may disturb the order in the array, like unset() or key manipulation.
& 运算符是指向数组中特定原始字符串的指针。(而不是该字符串的副本)您可以通过这种方式为数组中的字符串分配一个新值。你唯一不能做的就是任何可能扰乱数组顺序的事情,比如 unset() 或键操作。
The resulting array of the example above will be
上面例子的结果数组将是
$aArray = array('work','home', 0, 0)
回答by Muscaria
A bit other and much quicker way, but true, need a loop:
有点其他和更快的方式,但真的,需要一个循环:
//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);
Will output:
将输出:
Array
(
[0] => bar
[1] => market
[2] => 0
[3] => 0
[4] => sky
[5] => door
)
回答by javigzz
An alternative using array_map:
使用 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);
回答by fraktal12
you may try array_walk function:
你可以试试 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);
回答by MarkokraM
Just a small point to the for loop. Many dont realize the second comparing task is done every new iteration. So if it was a case of big array or calculation you could optimize loop a bit by doing:
只是for循环的一个小点。许多人没有意识到第二个比较任务是在每次新迭代中完成的。因此,如果是大数组或计算的情况,您可以通过执行以下操作来优化循环:
for ($i = 0, $c = count($asting); $i < $c; $i++) {...}
You may also want to see http://php.net/manual/en/function.array-replace.phpfor original problem unless the code really is final :)
您可能还想查看http://php.net/manual/en/function.array-replace.php以了解原始问题,除非代码确实是最终的 :)
回答by MarkokraM
You can also give array as a parameter 1 and 2 on str_replace...
您还可以在 str_replace 上将数组作为参数 1 和 2 提供...
回答by Vishal
Try This
尝试这个
$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);