Daniel
Daniel

Reputation: 376

PHP: Replace string found in array with another in other array

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

Upvotes: 0

Views: 145

Answers (1)

chris85
chris85

Reputation: 23892

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

Upvotes: 1

Related Questions