dany
dany

Reputation: 177

PHP search and replace between array and multidimensional array

I have input textarea, the text that I input is converted into $array like:

Array
(
    [0] => cat
    [1] => sat
    [2] => on
    [3] => the
    [4] => monkey
    [6] => is
    [7] => nice
    [8] => dog
    [9] => ate
    [10] => fish
)

And I have $another_array like:

Array
(
    [0] => Array
        (
            [id] => 1
            [word] => cat
            [keyword] => nice cat
        )

    [1] => Array
        (
            [id] => 2
            [word] => dog
            [keyword] => good dog
        )

    [2] => Array
        (
            [id] => 3
            [word] => monkey
            [keyword] => cute monkey
        )

    [3] => Array
        (
            [id] => 4
            [word] => fish
            [keyword] => fresh fish
        )

    [4] => Array
        (
            [id] => 5
            [word] => bird
            [keyword] => love bird
        )

    [5] => Array
        (
            [id] => 6
            [word] => rabbit
            [keyword] => rats
        )

    [6] => Array
        (
            [id] => 7
            [word] => animal
            [keyword] => not animal
        )

)

What I am trying to do is to replace every word in $array that matching [word] in $another_array and replace every words on textarea based [keyword] in $another_array

I tried to use str_replace, but the result is so messy.

The text I input in textarea

cat sat on the monkey, monkey is nice, dog ate fish

I want a result like

nice cat sat on the cute monkey, cute monkey is nice, good dog ate fresh fish

Upvotes: 0

Views: 99

Answers (4)

Rahul
Rahul

Reputation: 18557

Try this code,

foreach ($array as $k => $v) {
    if($a = array_search($v, array_column($another_array, 'word'))){
        $array[$k] = $another_array[$a]['keyword'];
    }
}
$str = implode(' ', $array);

Give it a try, this will work.

Upvotes: 1

Ganesh Radhakrishnan
Ganesh Radhakrishnan

Reputation: 350

If you didn't get any options please try this.

$array = array
(
    0 => 'cat',
    1 => 'sat',
    2 => 'on',
    3 => 'the',
    4 => 'monkey',
    6 => 'is',
    7 => 'nice',
    8 => 'dog',
    9 => 'ate',
    10 => 'fish'
);

$anotherarray = array
(
    0 => array('id' => 1,'word' => 'cat','keyword' => 'nice cat'),
    1 => array('id' => 2,'word' => 'dog','keyword' => 'good dog'),
    2 => array('id' => 3,'word' => 'monkey','keyword' => 'cute monkey'),
    3 => array('id' => 4,'word' => 'fish','keyword' => 'fresh fish'),
    4 => array('id' => 5,'word' => 'bird','keyword' => 'love bird'),
    5 => array('id' => 6,'word' => 'rabbit','keyword' => 'rats'),
    6 => array('id' => 7,'word' => 'animal','keyword' => 'not animal')
);

$concat = implode(' ', $array);

echo $concat.'<br/>';

foreach ($array as $key1 => $item) 
{
    foreach ($anotherarray as $key2 => $search) 
    {
        if(in_array($item, $search))
        {
            $array[$key1] = $search['keyword'];
        }
    }
}

$concat = implode(' ', $array);

echo $concat;

Upvotes: 1

LF-DevJourney
LF-DevJourney

Reputation: 28529

for performance you can save the map to avoid traverse another array everytime. you can use implode and explode to trans between string and array.

$value_map = [];
foreach($another_array as $v)
{
  $value_map[$k['word']] = $v['keyword'];
}
$result = array_map(function($v) use($value_map){return isset($value_map[$v]) ? $value_map[$v] : $v;}, $array);

Upvotes: 1

Parithiban
Parithiban

Reputation: 1656

Try This Code

$check = [
    ["id"=>1, "word"=>"dog", "keyword"=>"good dog"],
    ["id"=>2, "word"=>"monkey", "keyword"=>"cute monkey"],
    ["id"=>3, "word"=>"fish", "keyword"=>"fresh fish"]
];


$input = [
           'dog',
           'sat',
           'monkey',       
           'fish'       
        ];


$result =   array_map(function ($v) use ($value, $check) {
                $key = array_search($v, array_column($check, 'word'));
                if(is_numeric($key)){
                    return $v = $check[$key]['keyword'];
                }

                return $v;
            }, $input);

print_r($result);

echo implode(" ",$result);

Link : https://eval.in/737332

Upvotes: 1

Related Questions