0

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

1 Answer 1

1

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);
?>
Sign up to request clarification or add additional context in comments.

4 Comments

They are just random names to use them as an small example of what I want to accomplish that's actually more complex, since I'm in Mexico many cities and states have accents, and URL's don't accept this so what I'm doing is to replace accents with normal letters so url's look like this www.site.com/search/hey instead of www.site.com/search/héy ... But when I compare it to show my db results I just don't get anything, so I just want to replace it back to "héy".. BTW, your code does not work for me :/
hey doesn't become héy, or is it suppose to do something else?
I was asking about the color options because I didn't know if you wanted to substitute those as well. If so you could use array merge (I don't think str_replace works with multi-dimensional arrays).
Oh wait, sorry, it works. I was testing your code with an online compiler wich didn't worked. Tested locally and works. Thanks alot, now I'll see if it works for my current complex code. Again, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.