在php中将单词替换为点值


Replacing word with point value in php

我想从用户那里获取输入文本。我想将用户使用的单词与同样具有值的单词数据库进行比较。

现在,我制作了一个数组,它接受输入并将其运行到DB并返回值。我的问题是它的顺序不对。

    // GETTING THE ARRY OF WORDS FROM entry_body( $entry_text)
    $entry_text = $request->only('entry_body');
    // REMOVE THE KEY AND JUST GET THE STRING VALUE
    $entry_text = array_pull($entry_text,"entry_body");
    $entry_noTag = strip_tags($entry_text);
    // CONVERTE THE STRING ARRAY
    $entry_explode = explode(" ", $entry_noTag);
    // COUNT THE LENTH OF THE WORDS AND DIVIDE IT BY 3
    $entry_lenth = count($entry_explode);
    $new_array = array();
    // EACH WORD GETS ANALAYZED 
    $matched_words = Words::whereIn('word', $entry_explode)->get();
    foreach($matched_words as $mw){
            $new_array[]=$mw->word;
            $value_array[$mw->word]=$mw->value;
    }
    $arrayOfWords = array();
    foreach ($entry_explode as $word) {
        if (in_array($word,$new_array)) {
            $arrayOfWords['yes_words'][] = $word;
        } else {
            $arrayOfWords['no_word'][] = $word;
        }
    }

我还需要把单词按顺序分成三组。我这样做是为了让小组按正确的顺序排列。

    $entry_math = number_format($entry_lenth / 3);

    return array_chunk($entry_explode, $entry_math);

这是我得到的输出

     [
      [
        "Hey",
        "Jude,",
        "don't",
        "make",
        "it",
        "bad",
        "Take",
        "a",
        "sad",
        "song"
       ],
     [
        "and",
        "make",
        "it",
        "better",
        "Remember",
        "to",
        "let",
        "her",
        "into",
        "your"
       ],
       [
        "heart",
        "Then",
        "you",
        "can",
        "start",
        "to",
        "make",
        "it",
        "better."
       ]
      ]

我需要用从$value_array中得到的数字替换单词,但我不能100%确定应该如何做到这一点。

$value_array就是这样的。

 {
   "a": "0",
   "and": "0",
   "bad": "1",
   "better": "2",
   "can": "2",
   "heart": "3",
   "her": "3",
   "hey": "3",
   "into": "0",
   "it": "2",
   "let": "1",
   "make": "1",
   "remember": "3",
   "sad": "1",
   "song": "2",
   "start": "3",
   "take": "1",
   "then": "2",
   "to": "2",
   "you": "2",
   "your": "2"
 }

你可以用简单的foreach,来解决它

参见此示例,

考虑一下这个数组。

$input = ['hi', 'you', 'me', 'and','others'];
$database_input = [
    'hi' => 1,
    'you' => 5,
    'me' => 4,
    'and' => 3,
    'others' => 2,
    'some' => 6,
    'extra' => 7,
    'elements' => 8
];

要获得具有此输出的数组

Array
(
    [hi] => 1
    [you] => 5
    [me] => 4
    [and] => 3
    [others] => 2
)

你所需要做的就是

$input = ['hi', 'you', 'me', 'and','others'];
$database_input = [
    'hi' => 1,
    'you' => 5,
    'me' => 4,
    'and' => 3,
    'others' => 2,
    'some' => 6,
    'extra' => 7,
    'elements' => 8
];
$output = [];
foreach($input as $item) {
    if(array_key_exists(strtolower($item), $database_input))
        $output[strtolower($item)] = $database_input[strtolower($item)];
}

以下是做事的基本逻辑。

我已经创建了与您的输入一起工作的php粘贴,请参阅下面的链接:

http://sandbox.onlinephpfunctions.com/code/63c1a0eb669df43a92a15104340f528a76a2cfb0