且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:替换字符串数组发现另一个在其他阵列

更新时间:2022-11-11 11:47:11

你使用那些关于颜色选项什么呢?

 < PHP
$ theString =哎;美元=阵列(
    '动物'=>阵列('嗨','怎么样','是'),
    '颜色'=>阵列('你','好','谢谢')
);
$ B =阵列(
    '动物'=>阵列('嗨','怎么样','是'),
    '颜色'=>阵列('你','好','谢谢')
);
回声str_replace函数($ B ['动物'],$ A ['动物'],$ theString);
?>

I have one string variable and 2 arrays:

$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";

I'd like to find 'hey' in array $b (this already works with in_array_r() function) and once I've found it I'd like to replace it with the exact same key in array $a and set my variable

$theString to the value of the key in $a.

What I have so far:

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

What about the color options anything you're using those for?

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