PHP在数组和多维数组之间进行搜索和替换

时间:2017-02-15 08:23:11

标签: php arrays multidimensional-array str-replace

我输入textarea,我输入的文本转换为$array,如:

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

我有$another_array喜欢:

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
        )

)

我要做的是替换$array中匹配$another_array中[word]的所有单词,并替换$another_array中基于[keyword]的textarea上的每个单词

我尝试使用str_replace,但结果太乱了。

我在textarea中输入的文字

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

我想要一个像

这样的结果
nice cat sat on the cute monkey, cute monkey is nice, good dog ate fresh fish

4 个答案:

答案 0 :(得分:1)

试用此代码

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

链接:https://eval.in/737332

答案 1 :(得分:1)

对于性能,您可以保存地图以避免每次都遍历另一个数组。你可以使用implode和explode来在字符串和数组之间进行转换。

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

答案 2 :(得分:1)

如果您没有获得任何选择,请尝试此操作。

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

答案 3 :(得分:1)

试试这段代码,

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

试一试,这样可行。