基于其他数组替换数组中的值


Replace value in array based on other array

如果corrosconding数组包含某个值,我想替换多维数组的值。

基本上,我有两个多维数组。一个包含实际数据,另一个包含是否应修改第一个数组的是/否。

有什么方法可以做到这一点吗:

if optB[i][i] contains 'yes'
        then opt[i][i] = '<strong>'.opt[i][i].'</strong>';

我不知道这是否可能。如有任何帮助,我们将不胜感激——谢谢!

谢谢你到目前为止的帮助。这是阵列:

[opt] => Array
    (
        [0] => Array
            (
                [0] => value1
                [1] => value2
            )
        [1] => Array
            (
                [0] => value3
                [1] => value4
            )
    )
[optB] => Array
    (
        [0] => Array
            (
                [0] => on
            )
        [1] => Array
            (
                [1] => on
            )
    )

这些是一些有趣的数组,因为通常数字数组总是有一个0。我想你可能有一些不同的密钥组合,所以我认为这是最好的"经得起未来考验"的方法:

foreach ($optB as $i => $optB2) {
    foreach ($optB2 as $j => $val) {
        if ($val) {
            $opt[$i][$j] = '<strong>' . $opt[$i][$j] . '</strong>';
        }     
    }        
}

这是可能的。你可以这样做:

for ($i = 0; $i < count(opt); $i++) {
    if ($optB[$i][$i] == "yes")
        opt[$i][$i] = '<strong>'.opt[$i][$i].'</strong>';
}

可以这样写:

if (strpos($optB[$i][$i], 'yes'))
    $opt[$i][$i] = '<strong>'.$opt[$i][$i].'</strong>';

沿着这些线的东西:

foreach ($opt as $i => &$arr) {
    foreach ($arr as $j => &$val) {
        if ($optB[$i][$j]) {
            $val = "<strong>$val</strong>";
        }
    }
}

根据需要进行修改。