PHP:将数组中的字符串替换为其他数组中的另一个字符串

时间:2015-03-01 16:30:01

标签: php arrays search

我有一个字符串变量和2个数组:

$theString = "hey";

$a = array(
    'animal' => array('héy','hów','áre'),
    'color'  => array('yóu','good','thanks')
);
$b = array(
    'animal' => array('hey','how','are'),
    'color'  => array('you','good','thanks')
);
# OUTPUT SHOULD BE: $theString = "héy";

我想在数组$ b 中找到'hey'(这已经适用于in_array_r()函数),一旦我找到它,我想用精确的替换它数组$ a中的相同键并设置我的变量

$ theString为$ a中键的值。

到目前为止我有什么:

if (in_array_r($theString, $b)) {
    $key = array_search($theString, $b);
    $newarray = array_replace($b,$a);
    foreach($a as $name=>$value) {
        if(key($value)==$name)
            $city == $value;
    }
    #$city = $newarray[$state][$hey];
}

//FUNCTION FOR Searching in a multiple array... array('something'=>array('tata'=>'tata'))
function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

1 个答案:

答案 0 :(得分:1)

您使用它们的颜色选项怎么样?

<?php
$theString = "hey";

$a = array(
    'animal' => array('héy','hów','áre'),
    'color'  => array('yóu','good','thanks')
);
$b = array(
    'animal' => array('hey','how','are'),
    'color'  => array('you','good','thanks')
);
echo str_replace($b['animal'], $a['animal'], $theString);
?>