2011-03-03 56 views
9

想和0替换数组值,排除工作和家庭PHP - 取代数组值

array(work,home,sky,door); 

我想用PHP来取代这个数组值,

我有尝试使用此功能更换该阵列

$asting = array(work,home,sky,door); 
$a = str_replace("work","0",$asting); 

怎么样我的数组是无穷大,从提交的表单中添加,但我想更换值为0只排除工作&回家吗?

+0

所以你要更换'door'和'sky'用'0'? Ur没有设置它们? – alexn 2011-03-03 10:41:08

+0

是的,我想用0代替门和天空。 – wyman 2011-03-03 10:46:47

+0

你对'work'和''work''有区别吗?请引用你的字符串。 – deceze 2011-03-03 10:49:36

回答

-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; 
10

循环将多次执行一系列操作。因此,对于数组中的每个元素,都要检查它是否与要更改的元素相同,如果是,请更改它。此外,一定要放在周围的字符串引号

//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

但如果你是在东西挣扎,如循环,你可能想要阅读一些介绍性的编程材料。这应该可以帮助你真正理解发生了什么。

+0

你会想''asting [$ i] = 0;''。 – alexn 2011-03-03 14:08:04

+0

终于我的脚本已经工作了,感谢Jonno – wyman 2011-03-04 02:12:33

1

你也可以给数组参数1和2 str_replace函数...

+0

我的数组是如何从提交的表单添加无限的,但我想将值替换为0排除工作和家庭? – wyman 2011-03-03 14:03:58

1

只是一个小点的for循环。许多人都没有意识到第二个比较任务是在每次新的迭代中完成的。所以,如果是大数组或计算的情况下,你可以通过做了一些优化循环:

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

你也可以看到http://php.net/manual/en/function.array-replace.php原创问题,除非代码真的是最后:)

+0

但array_replace寻找“具有相同键值” – AdamS 2017-09-04 12:36:28

9

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

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

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

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

将所得的实施例的阵列上方将是

$aArray = array('work','home', 0, 0) 
2

有点其它和更快捷的方式,但真实的,需要一个循环:

//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 
) 
+0

不会在每个字符串内搜索str_replace?所以它也会用0代替'辛勤工作'和'寄宿家庭'。 – AdamS 2017-09-04 12:34:02

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); 
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);