PHP: 将数组中找到的字符串替换为另一个数组中的字符串
PHP: Replace string found in array with another in other array
我有一个字符串变量和两个数组:
$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;
}
您使用这些颜色选项的目的是什么?
<?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);
?>
我有一个字符串变量和两个数组:
$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;
}
您使用这些颜色选项的目的是什么?
<?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);
?>